Java中Runtime和Properties
2015-12-12 18:52
399 查看
1 RunTtme
它用来代表Java运行时的相关环境信息,它可以访问JVM的相关信息,如内存、处理器等。
2.Properties
用来记录系统的配置信息或者国际化时字段的匹配翻译
2.1 常用API
2.2 常用方法代码实例
它用来代表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 Propertiestable to the output stream in a format suitable for loading |
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();
}
}
}
相关文章推荐
- Java 类属性继承关系
- Java中获取文件大小的正确方法
- JDK的帧--java.util包装工具库
- Hadoop2.7 群集配置+eclipse开发环境搭建
- Leetcode: Spiral Matrix. Java
- myeclipse8.5注册码(20151212)
- Java的进制转换
- java命令执行jar包的方式
- Eclipse生成javadoc
- json详细介绍(for Java)
- 计时器
- java图形程序设计之面板图形绘制
- 在Eclipse中创建Maven多模块工程的例子
- 找出两个字符串的最长相同字符串及指定字符串频率
- java 学习笔记
- java格式化输出小数位数
- 简易计算器java版
- java源码分析之LinkedHashMap
- java图形程序设计框架下添加面板
- java框架源码之Quartz(1):定时任务如何调度