java.lang.System
提供了一些系统的静态对象和方法
1、类结构
public final class System
2、构造方法
private System() {
}
被私有化,也就是不能创建System类的对象
3、常用的静态变量
private static native void registerNatives();
static {
registerNatives();
}
public final static InputStream in = null;
public final static PrintStream out = null;
public final static PrintStream err = null;
public static void setIn(InputStream in) {
checkIO();
setIn0(in);
}
public static void setOut(PrintStream out) {
checkIO();
setOut0(out);
}
public static void setErr(PrintStream err) {
checkIO();
setErr0(err);
}
private static native void setIn0(InputStream in);
private static native void setOut0(PrintStream out);
private static native void setErr0(PrintStream err);
in、out、err分别是标准输入流、标准输出流和标准错误输出流。
具体是如何被初始化的不太清楚,按一些博客讲解,是通过registerNatives方法调用set方法完成的。
4、其它方法
(1)复制数组
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,int length);
几个参数的意义?
(2)系统时间
public static native long currentTimeMillis();
public static native long nanoTime();
(3)获取和设置系统属性
public static String getProperty(String key) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key);
}
public static String setProperty(String key, String value) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPermission(new PropertyPermission(key,
SecurityConstants.PROPERTY_WRITE_ACTION));
}
return (String) props.setProperty(key, value);
}
private static void checkKey(String key) {
if (key == null) {
throw new NullPointerException("key can't be null");
}
if (key.equals("")) {
throw new IllegalArgumentException("key can't be empty");
}
}