System 类
获取系统属性
1.1 私有构造方法
public final class System {
private System() {
}
}
1.2 System.out.println()
3 个类变量:in, out, err
public final static InputStream in = null;
public final static PrintStream out = null;
public final static PrintStream err = null;
2. 常用方法
- static native void arraycopy(...)
- static native long currentTimeMillis()
- static void gc()
- static void exit(int status)
- System.getProperties()
2.1 arraycopy(...)
数组复制
/**
* src – the source array.
* srcPos – starting position in the source array.
* dest – the destination array.
* destPos – starting position in the destination data.
* length – the number of array elements to be copied.
*/
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
int[] src = {1, 2, 3, 4, 5};
int[] desc = {10, 11, 12, 13};
System.arraycopy(src, 0, desc, 1, 2);
// [10, 1, 2, 13]
int[] desc2 = new int[10];
System.arraycopy(src, 1, desc2, 0, src.length - 1);
// [2, 3, 4, 5, 0, 0, 0, 0, 0, 0]
2.2 Arrays.copyOf(...)
public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
数组拷贝,返回一个新的数组
String[] strSrc = {"aaa", "bbb", "ccc", "ddd"};
String[] newArray = Arrays.copyOf(strSrc, 1);
// [bbb, ccc]
String[] newArray2 = Arrays.copyOfRange(strSrc, 1, 3);
2.3 currentTimeMillis()
可以用来计时,文件命名,随机数的种子数
使用一:计时
long start = System.currentTimeMillis();
// ...
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start));
使用二:获取系统当前时间
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
String now = format.format(date);
3. Properties类
Properties:属性集合
属性名和属性值都是字符串类型
系统属性 Properties properties = System.getProperties();
自定义属性
Properties properties = new Properties();
properties.setProperty("name", "tinyspot");
properties.setProperty("work", "Java");
Set<String> propertyNames = properties.stringPropertyNames();
for (String propertyName : propertyNames) {
}
和流有关的方法
PrintWriter out = new PrintWriter("demo.txt");
properties.list(out);
out.close();
FileOutputStream fos = new FileOutputStream("demo.properties");
properties.store(fos, "注释");
fos.close();
Properties properties2 = new Properties();
FileInputStream fis = new FileInputStream("demo.properties");
properties2.load(fis);
fis.close();
System.out.println(properties2.toString());
4. Scanner 控制台输入
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
public String next() {}
public int nextInt() {}
public double nextDouble() {}
若输入类型不匹配的数据,会抛异常 java.util.InputMismatchException
/**
* Math类在java.lang包下,所以不需要导包
* random():返回带正号的double值,该值大于等于0.0且小于1.0
* 更强的类 Random
* 可以直接返回整形类型的随机数 new Random().nextInt(int)
*/
public static void main(String[] args) {
int number = (int) (Math.random() * 100) + 1;
// 标准输入
Scanner scanner = new Scanner(System.in);
// String str = scanner.nextLine();
while (true) {
System.out.print("请输入你的数字: ");
int guess = scanner.nextInt();
// String name = input.next();
// ...
}
}
网友评论