标签:style blog ar io color os 使用 sp for
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.Enumeration; import java.util.Properties; public class test { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //a=111 //b=222 String realPath="d:\\p.txt"; //将properties文件加载到输入字节流中 InputStream is = new FileInputStream(realPath); //创建一个Properties容器 Properties prop = new Properties(); //从流中加载properties文件信息 prop.load(is); getPro(prop); export(prop); importer(prop); is.close(); } public static void getPro(Properties prop) throws Exception{ //读取某一key值 System.out.println(prop.getProperty("b")); //读取某一key值,没有则输出默认值 System.out.println(prop.getProperty("c","default")); //读取某一key值 System.out.println(prop.get("b")); //循环输出配置信息 for (Object key : prop.keySet()) { System.out.println(key + "=" + prop.get(key)); } prop.setProperty("d", "dddd"); System.out.println("setProperty后"); //循环输出配置信息 Enumeration e=prop.propertyNames(); while (e.hasMoreElements()) { System.out.println(e.nextElement()); } System.out.println("-------------------getPro-------------------"); } public static void export(Properties prop) throws IOException{ //定义一个输出流 OutputStream os1 = new FileOutputStream("d:\\ttt.xml"); OutputStream os2 = new FileOutputStream("d:\\ttt.properties"); //从Properties对象导出导出到xml prop.storeToXML(os1, "我从properties导出的XML配置文件"); //从Properties对象导出properties文件 prop.store(os2, "我从properties导出的properties配置文件"); os1.close(); os2.close(); System.out.println("-------------------export-------------------"); } public static void importer(Properties prop) throws Exception, FileNotFoundException, IOException{ //从xml加载配置信息,填充Properties容器 prop.loadFromXML(new FileInputStream("d:\\ttt.xml")); //循环输出配置信息 System.out.println("我从导出的xml加载配置文件信息!"); for (Object key : prop.keySet()) { System.out.println(key + "=" + prop.get(key)); } System.out.println("-------------------importer-------------------"); } }
输出
d:盘下写入的文件如下:
标签:style blog ar io color os 使用 sp for
原文地址:http://www.cnblogs.com/crazylqy/p/4173498.html