- 运行java 程序的参数:
public static void main(String[] args) --- args 是 参数
{
System.out.println(args.length);
for(String arg : args)
{
System.out.println(arg);
}
}
- Scanner 获取键盘输入 (需要引用:import java.util.Scanner;)
Scanner sc = new Scanner(System.in); //定义对象, system.in代表键盘输入
sc.useDelimiter(" "); //用回车来做分割符
while(sc.hasNext()) //是否还有下一个字符
{
System.out.println("The key output is:" + sc.next()); //输出下一个字符
}
Scanner sc1 = new Scanner(new File("Dog.java")); //读取文件内容 (File, 需要引用 import java.io.*;)
while(sc1.hasNextLine())
{
System.out.println(sc1.nextLine());
}
7.2 系统相关:
7.2.1 System: system类的 getenv(), getProperties(), getProperty() 方法,得到环境变量和系统属性
Properties props=System.getProperties(); //系统属性 (需要引用:import java.util.*;)
for(Enumeration e=props.propertyNames(); e.hasMoreElements();)
{
System.out.println((String) e.nextElement());
}
7.2.2 Runtime 类
通过Runtime rt = Runtime.getRuntime(); 来创建 runtime
Runtime rt = Runtime.getRuntime();
System.out.println(rt.availableProcessors());
System.out.println(rt.freeMemory());
System.out.println(rt.totalMemory());
rt.exec("notepad.exe");
7.3 常用类
7.3.1 object: 方法: equals(), finalize(), getClass(), toString(),
7.3.2 Objects 类: 也有上面类似的方法
System.out.println(b.toString());
System.out.println(Objects.toString(b));
7.3.3 String StringBuilder
String method:
String test = new String("This is one test");
System.out.println(test.concat(" Concat"));
System.out.println(test.startsWith("T"));
System.out.println(test.endsWith("t"));
System.out.println(test.equals(test.concat(" Concat")));
System.out.println(test.indexOf("T"));
System.out.println(test.lastIndexOf("is"));
System.out.println(test.replace("is"," "));
StringBuilder:
StringBuilder sb = new StringBuilder();
sb.append("Java");
sb.insert(0,"Hello ");
sb.replace(5, 6, ",");
sb.reverse();
System.out.println(sb.toString());
格式化字符串方法: MessageFormat.format("{0},{1}", "value 1","value 2"). 类似于C# string.format();
7.3.4 Date, Calendar
7.4 正则表达式
7.6 国际化:
Locale myLocal = Locale.getDefault(Locale.Category.FORMAT);
ResourceBundle bundle = ResourceBundle.getBundle("mess", myLocal);
System.out.println(bundle.getString("Hello"));
Mess_en_US.properties 内容: Hello = Welcome you!
格式化字符串方法: MessageFormat.format(msg, "value 1","value 2"). 类似于C# string.format();
数值格式化: NumberFormat
日期格式化: DateFormat
Random rand = new Random(); //import java.util.*;