6.6 System 类与 Runtime类

 

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());
 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值