与运行环境交互

1 java  类名  数组参数     java  My  qq "dw   dsfd" 

    多个参数中间空格隔开,如字符串中间有空格,加引号

2 使用Scanner获取键盘输入,基于正则表达式的文本扫描器,不同构造器可以接收文件,输入流,字符串为数据源 ,默认使用空白(空格,tab,回车)做分隔符,

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            System.out.println(sc.nextInt());
        }
    }
View Code

3 BufferedReader读键盘输入,io 流中字符,包装流  scanner是Java5新增,之前用他,只能一行一行读

    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while ((line = bf.readLine()) != null) {
            System.out.println(line);
        }
    }
View Code

4 system类代表当前Java程序运行平台,提供类field和方法,提供标准输入,输出,错误输出field, 访问环境变量,系统属性类方法,对象地址hash,当前系统时间

    public static void main(String[] args) throws Exception {
        Map<String, String> getenv = System.getenv();
        for (String name : getenv.keySet()) {
            System.out.println(name + "  " + getenv.get(name));
        }
        System.err.println(System.getProperty("os.name"));
    }
View Code

5 Runtime 代表Java程序运行时环境,可以访问jvm的相关信息,如处理器数量,内存信息

    public static void main(String[] args) throws Exception {
        Runtime r = Runtime.getRuntime();
        System.out.println(r.availableProcessors());
        System.out.println(r.freeMemory());
    }
View Code

6 克隆

自定义类实现克隆步骤 1 实现cloneable接口  2 自定义类实现clone方法,3 clone方法通过super。clone返回对象副本,是一种浅复制。对对象的各实例变量进行简单复制,如果是引用变量,只是简单的复制引用变量。指向同一个实例。

public class My {
    public static void main(String[] args) throws Exception {
        Address a = new Address();
        User u1 = new User("tom", a);
        User u2 = u1.clone();
        System.out.println(u1 == u2); false
        System.out.println(u1.address == u2.address); true    
    }
}

class Address {
    private String name = "henan";
}

class User implements Cloneable{
    String name;
    Address address;
    public User(String name, Address address) {
        super();
        this.name = name;
        this.address = address;
    }
    @Override
    public User clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return (User) super.clone();
    }
}
View Code

7 Random产生伪随机数的类,

    public static void main(String[] args) throws Exception {
        ThreadLocalRandom r = ThreadLocalRandom.current();
        System.out.println(r.nextInt(20));
    }
View Code
class Arith {
    private static int DEF_DIV_SCAL = 10;
    
    private Arith() {}
    
    public static double add(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.add(b2).doubleValue();
    }
    
    public static double div(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.divide(b2, DEF_DIV_SCAL).doubleValue();
    }
}
View Code

8 Date  类处理日期时间,大多已经过时

    public static void main(String[] args) {
        Date d1 = new Date(System.currentTimeMillis());
        Date d2 = new Date(System.currentTimeMillis() + 100);
        System.out.println(d1.before(d2));
    }
View Code

9 TineZone 时区

    public static void main(String[] args) {
        TimeZone t = TimeZone.getDefault();
        System.out.println(t.getDisplayName());
    }
View Code

10 正则:pattern对象是正则表达式编译后在内存中的表现形式,因此,正则表达式字符串必须被编译为Pattern对象,利用Pattern对象创建对应的Matter对象,

       Pattern p = Pattern.compile("a*b");
        Matcher m = p.matcher("aab");
        System.out.println(m.matches());
        
        System.out.println(Pattern.matches("a*b", "aab")); // 需要编译,效率不高
    public static void main(String[] args) {
        Pattern p = Pattern.compile("\\w+");
        Matcher m = p.matcher("java is easy");
        while (m.find()) {
            System.out.println("word  " + m.group() + " start " + m.start()+ "  end " + m.end());
        }
        Matcher m2 = p.matcher("tom my");
    }
View Code

11 国际化

resourceBundle 加载国家,语言资源包   locale 封装特定国家,语言环境  MessageFormat  格式化占位符字符串。

        Locale[] list = Locale.getAvailableLocales();
        for (Locale locale : list) {
            System.out.println(locale.getDisplayCountry() + "=" + locale.getCountry()
            + "  " + locale.getDisplayLanguage() + "=" + locale.getLanguage());
        }
View Code
        String a = "{0} dff {1}";
        System.out.println(MessageFormat.format(a, "tom", new Date()));
View Code

类文件代替资源文件

public class Mess_zh_CN extends ListResourceBundle{

    @Override
    protected Object[][] getContents() {
        // TODO Auto-generated method stub
        String[][] s = {{"hello", "zhongguo"}};
        return s;
    }

}
View Code

NumberFormat 和DateFormat是抽象类Format子类,用于实现数值,日期格式化,可将日期,数值转换字符串,也可以将字符串转换数值,日期

public static void main(String[] args) {
        double db = 1234000.33;
        Locale[] locales = {Locale.CHINA, Locale.JAPAN};
        NumberFormat[] nf = new NumberFormat[6];
        for (int i = 0; i < 2; i++) {
            nf[i * 3] = NumberFormat.getCurrencyInstance(locales[i]);
            nf[i * 3 + 1] = NumberFormat.getIntegerInstance(locales[i]);
            nf[i * 3 + 2] = NumberFormat.getPercentInstance(locales[i]);
        }
        for (int i = 0; i < 2; i++) {
            switch (i) {
            case 0:
                System.out.println("china");
                break;
            case 1:
                System.out.println("japan");
                break;
            }
            System.out.println(nf[i * 3].format(db));
            System.out.println(nf[i * 3 + 1].format(db));
            System.out.println(nf[i * 3 + 2].format(db));
        }
    }
View Code
    public static void main(String[] args) {
        Date d = new Date();
        Locale l = Locale.CHINA;
        System.out.println(DateFormat.getDateInstance(DateFormat.SHORT, l).format(d));
        System.out.println(DateFormat.getDateInstance(DateFormat.MEDIUM, l).format(d));
        System.out.println(DateFormat.getDateInstance(DateFormat.LONG, l).format(d));
        System.out.println(DateFormat.getDateInstance(DateFormat.FULL, l).format(d));
        
        System.out.println(DateFormat.getTimeInstance(DateFormat.SHORT, l).format(d));
        System.out.println(DateFormat.getTimeInstance(DateFormat.MEDIUM, l).format(d));
        System.out.println(DateFormat.getTimeInstance(DateFormat.LONG, l).format(d));
        System.out.println(DateFormat.getTimeInstance(DateFormat.FULL, l).format(d));
    }
17-9-18
2017-9-18
2017年9月18日
2017年9月18日 星期一
下午8:25
20:25:38
下午08时25分38秒
下午08时25分38秒 CST

 

 

posted on 2017-09-18 20:30  wheleetcode  阅读(202)  评论(0编辑  收藏  举报