System
类提供了访问系统相关信息和操作的方法,例如标准输入、输出、错误流、系统属性等。以下是 JDK 8 中 System
类的部分源代码:
public final class System {
private System() {}
public static final InputStream in = null;
public static final PrintStream out = null;
public static final PrintStream err = null;
public static void setIn(InputStream inStream) {
throw new UnsupportedOperationException("Method not supported");
}
public static void setOut(PrintStream outStream) {
throw new UnsupportedOperationException("Method not supported");
}
public static void setErr(PrintStream errStream) {
throw new UnsupportedOperationException("Method not supported");
}
public static native long currentTimeMillis();
public static native long nanoTime();
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
public static native int identityHashCode(Object x);
public static Properties getProperties() {
throw new UnsupportedOperationException("Method not supported");
}
public static String getProperty(String key) {
throw new UnsupportedOperationException("Method not supported");
}
public static String getProperty(String key, String def) {
throw new UnsupportedOperationException("Method not supported");
}
public static String setProperty(String key, String value) {
throw new UnsupportedOperationException("Method not supported");
}
public static String clearProperty(String key) {
throw new UnsupportedOperationException("Method not supported");
}
public static String getenv(String name) {
throw new UnsupportedOperationException("Method not supported");
}
public static Map<String, String> getenv() {
throw new UnsupportedOperationException("Method not supported");
}
public static void exit(int status) {
throw new UnsupportedOperationException("Method not supported");
}
public static void gc() {
throw new UnsupportedOperationException("Method not supported");
}
public static void runFinalization() {
throw new UnsupportedOperationException("Method not supported");
}
public static void load(String filename) {
throw new UnsupportedOperationException("Method not supported");
}
public static void loadLibrary(String libname) {
throw new UnsupportedOperationException("Method not supported");
}
public static String mapLibraryName(String libname) {
throw new UnsupportedOperationException("Method not supported");
}
public static void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length)
{
throw new UnsupportedOperationException("Method not supported");
}
public static int identityHashCode(Object x) {
throw new UnsupportedOperationException("Method not supported");
}
public static Console console() {
throw new UnsupportedOperationException("Method not supported");
}
public static Channel inheritedChannel() throws IOException {
throw new UnsupportedOperationException("Method not supported");
}
public static Path getTemporaryDirectory() {
throw new UnsupportedOperationException("Method not supported");
}
public static SecurityManager getSecurityManager() {
throw new UnsupportedOperationException("Method not supported");
}
public static void setSecurityManager(SecurityManager s) {
throw new UnsupportedOperationException("Method not supported");
}
}
System
类包含了一些静态方法,可以用来获取系统属性、当前时间、标准输入输出流、执行垃圾回收等。不过,在实际应用中,通常会使用更高级的工具类来代替 System
类的部分功能,比如使用 java.util.logging.Logger
代替 System.out.println
来进行日志输出。