System类
System类代表当前程序运行平台,提供了代表标准输入(System.in)输出(System.out)和错误输出(System.err)的类属性;并提供一些静态方法用于访问环境变量、系统属性;还提供了加载文件和动态链接库的方法
示例代码:(访问环境变量和系统属性)
public class TestSystem {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
//获取系统所有的环境变量
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 pro = System.getProperties();
//将所有属性保存到pro.txt中
pro.store(new FileOutputStream("pro.txt"), "System Properties");
//输出特定的系统属性
System.out.println(System.getProperty("os.name"));
}
}
Runtime类
Runtime类代表Java程序的运行时环境,每个Java程序都有一个与之对应的Runtime实例,应用程序通过该对象与其运行时环境相连,该类可以访问JVM的相关信息,如处理器数量、内存信息等。
示例代码:public class TestRuntime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Runtime rt = Runtime.getRuntime();
System.out.println("处理器数量:"+rt.availableProcessors());
System.out.println("空闲内存数:"+rt.freeMemory() );
System.out.println("总内存数:"+rt.totalMemory());
System.out.println("可用最大内存数:"+rt.maxMemory());
}
}
Java系统与运行时环境
本文介绍了Java中的System类和Runtime类,展示了如何使用System类访问环境变量和系统属性,以及如何利用Runtime类获取关于Java虚拟机运行时环境的信息,包括处理器数量、内存使用情况等。
2473

被折叠的 条评论
为什么被折叠?



