Java中Runtime和Properties

1 RunTtme

它用来代表Java运行时的相关环境信息,它可以访问JVM的相关信息,如内存、处理器等。


Runtime rt = Runtime.getRuntime();
	System.out.println("-->"+rt.availableProcessors());//处理器信息
	System.out.println("-->"+rt.freeMemory()); //空闲内存
	System.out.println("-->"+rt.maxMemory());  //最大内存
	try {
		rt.exec("notepad.exe"); //打开记事本
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

2.Properties

用来记录系统的配置信息或者国际化时字段的匹配翻译

2.1 常用API


修饰符与类型方法与描述
StringgetProperty(String key)
Searches for the property with the specified key in this property list.
StringgetProperty(String key, String defaultValue)
Searches for the property with the specified key in this property list.
voidlist(PrintStream out)
Prints this property list out to the specified output stream.
voidlist(PrintWriter out)
Prints this property list out to the specified output stream.
voidload(InputStream inStream)
Reads a property list (key and element pairs) from the input byte stream.
voidload(Reader reader)
Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
voidloadFromXML(InputStream in)
Loads all of the properties represented by the XML document on the specified input stream into this properties table.
Enumeration<?>propertyNames()
Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.
voidsave(OutputStream out, String comments)
Deprecated. 
This method does not throw an IOException if an I/O error occurs while saving the property list. The preferred way to save a properties list is via the store(OutputStream out, String comments) method or the storeToXML(OutputStream os, String comment) method.
ObjectsetProperty(String key, String value)
Calls the Hashtable method put.
voidstore(OutputStream out, String comments)
Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading

2.2 常用方法代码实例

package SomeSystemClassLearning;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;


public class SomeSysemClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
    System.out.println("args长度:"+args.length);
    for(String arg:args)
    {
    	System.out.println("arg=="+arg);
    }
    
    Scanner scan = new Scanner(System.in);
   
    //只是将回车作为换行符
//    scan.useDelimiter("\n");
//    while(scan.hasNext())
//    {
//    	System.out.println("键盘输入的数为:"+scan.next());
//    }
    
    //只有输入整数才会执行,否则会异常退出
//    while(scan.hasNextInt())
//    {
//    	System.out.println("键盘输入的整数是:"+scan.nextInt());
//    }
    try {
    	File file = new File("E:\\20151011\\sff.txt");
    	Scanner fileSc = new Scanner(file);
    	  while(fileSc.hasNextLine())
    	    {
    	    	System.out.println(fileSc.nextLine());
    	    }
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	Map<String,String> sysMap = System.getenv();
	File fileEnv = null;
	OutputStream fileOutPut = null;
	try {
		fileEnv = new File("E:"+File.separator+"20151202"+File.separator+"env.txt");
		if(!fileEnv.exists())
		{
			fileEnv.createNewFile();
		}
		fileOutPut = new FileOutputStream(fileEnv,true);
		for(String name:sysMap.keySet())
		{
//		System.out.println(name+"------>"+sysMap.get(name));
			fileOutPut.write(name.getBytes());
			fileOutPut.write("----->".getBytes());
			fileOutPut.write(sysMap.get(name).getBytes());
			fileOutPut.write('\r');
			fileOutPut.write('\n');
			
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	finally
	{
		try {
			fileOutPut.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	//也可以通过Properties文件写入
	Properties  pop = System.getProperties();
	try {
		pop.store(new FileOutputStream("D:/workspace/Test/src/configure/SomEnv.properties"),"SomEnvKeys") ;
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值