Java Property类文件配置

来源:互联网 发布:游戏视频制作软件 编辑:程序博客网 时间:2024/05/20 19:28
package testJavaProperty;import java.io.File;import java.io.IOException;import java.io.PrintStream;import java.util.Properties;public class TestProperty {public static void main(String[] args){//读取.property文件//File pFile = new File("test.properties");//FileInputStream pInStream = null;////try {//pInStream = new FileInputStream(pFile);//} catch (FileNotFoundException e) {//e.printStackTrace();//}////Properties p = new Properties();//try {//p.load(pInStream);//} catch (IOException e) {//e.printStackTrace();//}////Enumeration enu = p.propertyNames();//取出所有的key//p.list(System.out);//System.out.println("\n\n");//while(enu.hasMoreElements()){//String thisKey = (String)enu.nextElement();//System.out.println("key="+thisKey);//System.out.println("Value="+p.getProperty(thisKey));//}//写入.property文件Properties p = new Properties();p.setProperty("name", "China");p.setProperty("location", "Asian");p.setProperty("population", "14");try {PrintStream out = new PrintStream(new File("test.properties"));p.list(out);} catch (IOException e) {e.printStackTrace();}}}package testJavaProperty;import java.io.File;import java.io.IOException;import java.io.PrintStream;import java.util.Properties;public class TestPropertyXml {public static void main(String[] args) {//读取XML文件//File pFile = new File("test.xml");//FileInputStream pInStream = null;//try {//pInStream = new FileInputStream(pFile);////Properties p = new Properties();//p.loadFromXML(pInStream);//p.list(System.out);//} catch (IOException e) {//e.printStackTrace();//}//写入XML文件Properties p = new Properties();p.setProperty("name", "China");p.setProperty("location", "Asian");p.setProperty("population", "14");try {PrintStream out = new PrintStream(new File("test.xml"));p.storeToXML(out, "test");} catch (IOException e) {e.printStackTrace();}}}

XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>test</comment>
<entry key="location">Asian</entry>
<entry key="population">14</entry>
<entry key="name">China</entry>
</properties>

0 0