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
修饰符与类型 | 方法与描述 |
---|---|
String | getProperty(String key)
Searches for the property with the specified key in this property list.
|
String | getProperty(String key, String defaultValue)
Searches for the property with the specified key in this property list.
|
void | list(PrintStream out)
Prints this property list out to the specified output stream.
|
void | list(PrintWriter out)
Prints this property list out to the specified output stream.
|
void | load(InputStream inStream)
Reads a property list (key and element pairs) from the input byte stream.
|
void | load(Reader reader)
Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
|
void | loadFromXML(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.
|
void | save(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.
|
Object | setProperty(String key, String value)
Calls the
Hashtable method
put .
|
void | store(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();
}
}
}