Java I/O 流操作(一)System、Properties、Runtime 类

 

System 类

 
我们先看下 Java 官方文档对 System 类的描述:
 
 
The System class contains several useful class fields and methods. It cannot be instantiated.

Among the facilities provided by the System class are standard input, standard output, 

and error output streams; access to externally defined properties and environment variables; 

a means of loading files and libraries; and a utility method for quickly copying a portion of an array.

 

下面就对上面的 Java API 描述做简单的介绍,这个 System 类包含了一些有用的服务类字段和方法,它不能实例化,也就是说他的构造方法是私有的。它提供了标准的输入输出流和错误输出流;访问系统的属性和环境变量的功能;加载文件和库;以及更加高效的数组拷贝功能。

System 常用的 API 有:

 
  • err 标准错误输出流
  • in 标准输入流
  • out 标准输出流
  • getenv() 获取当前系统的环境变量
  • getProperties 获取当前系统属性
  • getProperty(String key) 根据 key 获取系统属性
  • getProperty(String key, String def) 根据 key 获取系统属性,提供默认值
  • load(String filename) 通过文件名称加载 Native 库
  • loadLibrary(String libname) 通过 libname 加载 Native 库
  • arraycopy(Object src,  int srcPos,Object dest, int destPos, int length) 数组拷贝

 

下面就上面的的功能来做一些具体的应用。

Demo1 获取系统属性:

 
 
Properties properties = System.getProperties();
//获取所有的系统环境变量
for (Object key : properties.keySet()) {
    String value = (String)properties.get(key);
    System.out.println(key+" ---> "+value);
}
//获取某个系统环境变量
String value = properties.getProperty("java.vm.version");
System.out.println(value);

//自定义一些系统环境变量
properties.setProperty("myPro", "my system properties");
System.out.println(properties.getProperty("myPro"));

 

Demo2 获取系统环境变量:
 

public class TestSystem {
    public static void main(String[] args) throws Exception {
        //获取系统所有的环境变量
        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"));
    }
}

 

Demo3 数组拷贝:

System.arraycopy(Object src,  int srcPos,Object dest, int destPos, int length) 参数意义:

  • src 源数组
  • srcPos 从 src 的 srcPos 下标处开始复制
  • dest 目标数组
  • destPos 复制到目标数组的 destPos 下标处
  • length 复制的长度
public static void main(String[] args) {
	int[] src = {11, 12, 13, 14, 15, 16};
	System.out.println(Arrays.toString(src));

	int[] dest = new int[10];

	//源数组下标 2 处开始(包括 2)复制 2 个数至目标数组下标 0 处
	System.arraycopy(src, 2, dest, 0, 2);
	System.out.println(Arrays.toString(dest));
}

// 输出结果:
[11, 12, 13, 14, 15, 16]
[13, 14, 0, 0, 0, 0, 0, 0, 0, 0]

需要注意的是:arraycopy 是浅拷贝。

arraycopy  的实现是 Native 实现的,所以当我们拷贝的数组比较大时,arraycopy 方法效率比其他的方式要高。

 

Demo4 控制台标准输入输出流 System.in/out:

public static void main(String[] args) throws IOException {
	InputStream is = System.in;
	InputStreamReader isr = new InputStreamReader(is);
	BufferedReader br = new BufferedReader(isr);
	String line;
	while ((line = br.readLine()) != null) {
		if (line.equals("over")) {
			break;
		}
		System.out.println("receive:" + line.toUpperCase());
	}

}

// 控制台测试:

111
receive:111
222
receive:222
over

Process finished with exit code 0

 

Properties 类

Properties 类的继承关系如下所示:

java.lang.Object
    java.util.Dictinary
        java.util.Hashtable
            java.util.Properties

 

Properties 是 Hashtable 的子类,也就是说它具备 Map 结构的特点,而且它里面存储的键值对都是字符串

它是集合和 I/O 相结合的集合容器,该对象的特点是可以用于键值对形式的配置文件。

Properties 常用 API:

  • getProperty(String key) 根据 key 从 Properties 对象中获取值
  • setProperty(String key,String value) 为 Properties 对象设置 key 和 value
  • store(OutputStream out,String comments) 把 Properties 对象储存到一个文件里面去,底层通过字节输出流处理
  • store(Writer writer,String comments) 这是 JDK1.6 后才有的,直接使用字符输出流就可以把 Properties 对象储存到文件中
  • load(InputStream inStream) 读取属性列表从字节输入流中,加载某个键值对文件,使得 Properties 与该文件相关联
  • load(Reader reader) 这是 JDK1.6 后才出现的,读取属性列表从字符输入流中
 
案例:使用 Properties 类来统计次数
 

public static void count()throws IOException{
    Properties properties =new Properties();
    File file =new File("count.ini");
    if(!file.exists()){
        file.createNewFile();
    }
    InputStream is =new FileInputStream(file);
    properties.load(is);
    String times = properties.getProperty("times");
    int count = 0;
    if(times!=null){
        count = Integer.parseInt(times);
        if(count>=5)
        System.out.println("你使用的次数已到,请续费!");
    }
    count++;
    OutputStream os =new FileOutputStream(file);
    properties.setProperty("times", count+"");
    properties.store(os,"count times");
    os.close();
    is.close();
}

 

 

Runtime 类

 
 
 
Runtime 代表 Java 程序的运行是环境,每一个 Java 程序都有一个与之对应的 Runtime 实例 ,应用程序通过该对象与运行时环境相连。正如 API 文档所说:
 
Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. 

The current runtime can be obtained from the getRuntime method.

An application cannot create its own instance of this class.

从上可得知,该类是不能直接实例化的,但是可以通过 getRumtime 方法获取实例

既然一个 Java 程序对应一个 Runtime 所以在一个 Java 程序中只能存在一个 Runtime,Runtime 是单例的.

查看 API 可以得到一些常用的方法:

 

public class TestRuntime {
    public static void main(String[] args) {
        Runtime rt = Runtime.getRuntime();
        System.out.println("处理器数量:" + rt.availableProcessors());
        System.out.println("空闲内存数:" + rt.freeMemory());
        System.out.println("总内存数:" + rt.totalMemory());
        System.out.println("可用最大内存数:" + rt.maxMemory());
    }
}

 

而且还可以打开应用程序:
 
 
    public static void main(String[] args) throws IOException {
        Runtime rt = Runtime.getRuntime();
        // 打开记事本软件
        Process p = rt.exec("notepad.exe");

        // 记事本打开文件,文件需要时绝对路径
        rt.exec("notepad.exe D:\\SystemCopy.java");
    }

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chiclaim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值