properties类及其子类用法
来源:互联网 发布:vb简单程序编写 编辑:程序博客网 时间:2024/05/29 18:07
在读取系统属性,环境变量,properties文件,简单的xml文件中,我们经常使用properties类
package demo4;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FilterOutputStream;import java.io.IOException;import java.util.Enumeration;import java.util.InvalidPropertiesFormatException;import java.util.Map;import java.util.Map.Entry;import java.util.Properties;public class Test1 {/*读取环境变量*/public void environment(){Map<String,String> env=System.getenv();for(Entry<String, String> entry:env.entrySet()){System.out.println("key:"+entry.getKey()+"\t");System.out.println("value:"+entry.getValue()+"\n");}System.out.println("value:"+System.getenv("JAVA_HOME"));}/*读取系统属性*/public void systemProperty(){Properties prop = System.getProperties();// 列出全部系统属性内容for (Entry<Object, Object> entry : prop.entrySet()) {System.out.print("KEY: " + entry.getKey() + "\t");System.out.println("VALUE: " + entry.getValue());}// prop.list(System.out); // 该方法也可以列出全部系统属性// 获得指定系统属性内容String version = prop.getProperty("java.version");System.out.println(version);String dir = prop.getProperty("user.dir");System.out.println("dir="+dir);// 设置系统属性setProperty("mykey", "myvalue");System.out.println(prop.getProperty("mykey", "default"));// 列出全部系统属性名Enumeration<String> names = (Enumeration<String>) prop.propertyNames();while (names.hasMoreElements()) {String s = names.nextElement();System.out.println(s);}}private static void setProperty(String key, String value) {Properties prop = System.getProperties();prop.setProperty(key, value);}/*读取 properties文件*/public void propertyFile() throws FileNotFoundException, IOException{Properties prop=new Properties();File input=new File("test1.properties");prop.load(new FileInputStream(input));//prop.load(new FileReader(input));System.out.println(prop.getProperty("key1"));File output=new File("test2.properties");prop.setProperty("czk","123");prop.store(new FileOutputStream(output), "first record");// prop.store(new FileWriter(output), "属性列表的描述");}/*只能读取简单的xml文件*/public void xmlFile() throws InvalidPropertiesFormatException, FileNotFoundException, IOException{Properties prop = new Properties();File input = new File("config.xml");prop.loadFromXML(new FileInputStream(input));prop.setProperty("key", "value");File output = new File("new.xml");prop.storeToXML(new FileOutputStream(output), "属性列表的描述");}}
0 0
- properties类及其子类用法
- PacketFilter 包过滤类及其子类用法
- PacketFilter 包过滤类及其子类用法
- CharSequence及其子类用法总结
- list及其子类的用法
- Format类及其子类功能和用法详解
- Java reflect Type类及其子类用法分析
- HashTable 的子类 Properties类
- Student类及其子类
- Properties子类
- Android Drawable常见子类及其用法整理
- 关于Format类及其子类
- Java Throwable类及其子类
- Time、 Date类及其子类
- properties类是Hashtable的子类
- Properties的用法 Map映射 集合下的子类 Hashtable 的子类
- Properties类的用法
- 类 Properties 的用法
- HV000030: No validator could be found for type: java.lang.Integer.
- edittext实现自动查询,刷新listview
- Php websocket
- 16周(指针调用函数)
- 实现Reapter控件数据导出功能
- properties类及其子类用法
- 第十六周OJ项目四指针引出奇数因子
- java float计算精度丢失问题剖析
- Android 事件分发机制
- IOS GDC学习之 —— dispatch_semaphore
- HTML<marquee>标签
- 层叠样式表CSS
- 逐渐浮现水面的我
- jQuery序列化表单 serialize() serializeArray()