java-System类学习

1、System类是一个不能被实例化的类(私有了构造方法)

public final class System extends Object

System提供了一些有用的字段和方法

2、静态字段

public final static InputStream in = null;
public final static PrintStream out = null;
public final static PrintStream err = null;

标准输入流 System.in,常用于

Scanner sc = new Scanner(System.in);//用于接收用户输入数据

标准输出流 System.out,常用于输出

System.out.println();

3、静态方法

  • 给静态字段赋值

  • property存储相关

 public static void main(String[] args) {

        /**
         * static String        getProperty(String key) 获取指定键指示的系统属性。
         * static Properties    getProperties() 确定当前的系统属性。
         * static String        getProperty(String key, String def) 获取指定键指示的系统属性。
         *                      def 默认值,获取不到就返回def
         *
         * static String        setProperty(String key, String value)
         * static void          setProperties(Properties props) 
         *
         */
        System.setProperty("aaa", "AAA");
        System.out.println("aaa = " + System.getProperty("aaa"));
        System.out.println("ccc = " + System.getProperty("ccc", "CCC"));
        System.out.println("aaa = " + System.getProperty("aaa", "AAAAAA"));
        Properties in = new Properties();
        in.setProperty("bbb", "BBB");
        System.setProperties(in);

        Properties properties = System.getProperties();
        // 遍历properties
        Enumeration<?> names = properties.propertyNames();
        while (names.hasMoreElements()) {
            Object key = names.nextElement();
            Object val = properties.get(key);
            System.out.println(key + " = " + val);
        }
        // 删除property
        System.out.println(System.clearProperty("aaa"));
        System.out.println("aaa=" + System.getProperty("aaa"));
    }
/** 结果
aaa = AAA
ccc = CCC
aaa = AAA
bbb = BBB
null
aaa=null
**/
  • currentTimeMillis()和nanoTime()

        //currentTimeMillis()获取当前时间的毫秒值,可以跟Date,Calendar进行交换
        System.currentTimeMillis();
        /*
         以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。
         一般用来测量时长 
          */
        System.nanoTime();

nanoTime()详细解释

  • 其他

        // System.lineSeparator()返回与系统相关的行分隔符字符串。
        String separator = System.lineSeparator();
        System.out.println("换行符:" + separator + "=======");

        // 环境变量相关
        Map<String, String> envs = System.getenv();
        // 遍历
        for (Map.Entry<String, String> entry : envs.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        // 获取指定环境变量
        String java_home = System.getenv("JAVA_HOME");
        System.out.println("javahome==="+java_home);

输出结果:

System.gc() 运行垃圾回收器,调用时虚拟机并不会立即执行垃圾回收。

System.exit(int status) 终止当前运行的虚拟机

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值