System类的常用方法

首先来看看System类,该类位于java.lang包下面,直接继承Object类,是一个final类,不能被继承,不能实例化,都是静态属性和方法。
字段摘要:
static PrintStream err    标准错误输出
static InputSteam in    标准输入
static printStream out    标准输出

主要方法:
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 从指定源数组中复制一个数组
static long currentTimeMillis()    返回以毫秒为单位的当前时间
static void exit(int status)    终止当前Java虚拟机
static void gc()    运行垃圾回收器,不一定马上回收
static String getenv(String name)    获取指定的环境变量值
static Properties getProperties()    确定当前的系统属性
static int identityHashCode(Object x)    返回给定对象的哈希码
static void loadLibrary(String libname)     加载由 libname 参数指定的系统库
static long nanoTime()    返回最准确的可用系统计时器的当前值,以毫微秒为单位



import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Properties;




/**
 * System的常用方法
 *
 */
public class SystemTest {


public static void main(String[] args) {
/* 
*  System.out和System.err的区别:
*  1.err是立刻刷新缓冲区并输出,out则是先保存在缓冲区中并等缓冲区满或者有换行符才输出
*  2.out有可能会被缓冲,err会马上显示,所以err更适合调试
*  3.err和out的输出顺序
*/
System.out.println("hello");
System.err.println("world");
// try {
// //把打印的内容重定向到txt文件中
// System.setOut(new PrintStream(new FileOutputStream(new File("c:/test.txt"))));
// System.out.print("hello test.txt");
// System.setErr(new PrintStream(new FileOutputStream(new File("c:/err.txt"))));
// System.err.print("hello err.txt");
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
//产生一个自1970年1月1日0时起到当前的毫秒
System.out.println(System.currentTimeMillis());
// 返回最准确的可用系统计时器的当前值,以毫微秒为单位
System.out.println(System.nanoTime());//此方法只能用于测量已过的时间
long start = System.nanoTime();
long end = 0;
int i = Integer.MAX_VALUE;
System.out.println("MAX_VALUE=="+i);
while(true){
i--;
if(i<=0){
end = System.nanoTime()-start;
break;
}
}
System.out.println("循环执行了:"+end+"毫秒");
//获取指定的环境变量值
System.out.println(System.getenv("JAVA_HOME"));
Properties props = System.getProperties(); //系统属性
System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));
   System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));
   System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));
   System.out.println("Java的安装路径:"+props.getProperty("java.home"));
   System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version"));
   System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor"));
   System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));
   System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));
   System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));
   System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));
   System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version"));
   System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender"));
   System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name"));
   System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));
   System.out.println("Java的类路径:"+props.getProperty("java.class.path"));
   System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));
   System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));
   System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));
   System.out.println("操作系统的名称:"+props.getProperty("os.name"));
   System.out.println("操作系统的构架:"+props.getProperty("os.arch"));
   System.out.println("操作系统的版本:"+props.getProperty("os.version"));
   System.out.println("文件分隔符:"+props.getProperty("file.separator"));   //在 unix 系统中是"/"
   System.out.println("路径分隔符:"+props.getProperty("path.separator"));   //在 unix 系统中是":"
   System.out.println("行分隔符:"+props.getProperty("line.separator"));   //在 unix 系统中是"/n"
   System.out.println("用户的账户名称:"+props.getProperty("user.name"));
   System.out.println("用户的主目录:"+props.getProperty("user.home"));
   System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));
   
   
   //返回对象的哈希码
   System.out.println("hashCode=="+System.identityHashCode(new SystemTest()));
   
   
   //终止虚拟机,非 0 的状态码表示异常终止
System.exit(0);

}


}

转载于:https://my.oschina.net/u/565031/blog/168633

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值