系统相关类
System类
Map<String, String> env = System.getenv(); for (String name:env.keySet()){ System.out.println(name + "->" + env.get(name)); } //获取指定环境变量的值 System.out.println(System.getenv("JAVA_HOME")); // 获取所有的系统属性 Properties props = System.getProperties(); // 将所有系统属性保存到props。txt 文件中 props.store(new FileOutputStream("props.txt"),"System properties"); // 输出特定的系统属性 System.out.println(System.getProperty("os.name"));
System.identityHashCode
String s1 = new String("hello"); String s2 = new String("hello"); System.out.println(s1.hashCode() + "----"+ s2.hashCode()); // 不是同一个对象 identityHashCode值不一样 System.out.println(System.identityHashCode(s1) + "--" + System.identityHashCode(s2)); // 同一个字符对象 identityHashCode值一样 String s3 = "java"; String s4 = "java"; System.out.println(System.identityHashCode(s3) + "--" + System.identityHashCode(s4));
Runtime类
Runtime rt = Runtime.getRuntime(); System.out.println("处理器数量:" + rt.availableProcessors()); System.out.println("空闲内容数:" + rt.freeMemory() ); System.out.println("总内存数" + rt.totalMemory()); System.out.println("可用组大内存数" + rt.maxMemory());
// 单独启动一个进程来运行操作系统命令 Runtime rt = Runtime.getRuntime(); rt.exec("notepad.exe");