欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
运行清单 6 中的程序产生与原来的程序相同的输出。
保存 XML 属性
新的 Properties 还有一个功能是将属性存储到 XML 格式的文件中。虽然 store() 方法仍然会创建一个类似 清单 1 所示的文件,但是现在可以用新的 storeToXML() 方法创建如 清单 5 所示的文件。只要传递一个 OutputStream 和一个用于注释的 String 就可以了。清单 7 展示了新的 storeToXML() 方法。
清单 7. 将 Properties 存储为 XML 文件
Java代码
import java.util.*;
import java.io.*;
public class StoreXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
prop.setProperty(”one-two”, “buckle my shoe”);
prop.setProperty(”three-four”, “shut the door”);
prop.setProperty(”five-six”, “pick up sticks”);
prop.setProperty(”seven-eight”, “lay them straight”);
prop.setProperty(”nine-ten”, “a big, fat hen”);
FileOutputStream fos =
new FileOutputStream(”rhyme.xml”);
prop.storeToXML(fos, “Rhyme”);
fos.close();
}
}
import java.util.*; import java.io.*; public class StoreXML { public static void main(String args[]) throws Exception { Properties prop = new Properties(); prop.setProperty(”one-two”, “buckle my shoe”); prop.setProperty(”three-four”, “shut the door”); prop.setProperty(”five-six”, “pick up sticks”); prop.setProperty(”seven-eight”, “lay them straight”); prop.setProperty(”nine-ten”, “a big, fat hen”); FileOutputStream fos = new FileOutputStream(”rhyme.xml”); prop.storeToXML(fos, “Rhyme”); fos.close(); } }
运行清单 7 中的程序产生的输出如清单 8 所示。
清单 8. 存储的 XML 文件
Java代码
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE properties SYSTEM “http://java.sun.com/dtd/properties.dtd”>
<properties>
<comment>Rhyme</comment>
<entry key=”seven-eight”>lay them straight</entry>
<entry key=”five-six”>pick up sticks</entry>
<entry key=”nine-ten”>a big, fat hen</entry>
<entry key=”three-four”>shut the door</entry>
<entry key=”one-two”>buckle my shoe</entry>
</properties>
<?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE properties SYSTEM “http://java.sun.com/dtd/properties.dtd”> <properties> <comment>Rhyme</comment> <entry key=”seven-eight”>lay them straight</entry> <entry key=”five-six”>pick up sticks</entry> <entry key=”nine-ten”>a big, fat hen</entry> <entry key=”three-four”>shut the door</entry> <entry key=”one-two”>buckle my shoe</entry> </properties>
结束语
使用 XML 文件还是使用老式的 a=b 类型的文件完全取决于您自己。老式文件从内存的角度看肯定是轻量级的。不过,由于 XML 的普遍使用,人们会期望 XML 格式流行起来,因为它已经被广泛使用了,只不过没有用到 Properties 对象。选择完全在您。分析软件包 private XMLUtils 类的源代码以获得关于所使用的 XML 解析的更多信息。
Java代码
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 实现properties文件的读取
* @author bbflyerwww
* @date 2006-08-02
*/
public class PTest {
public static void main(String[] args) {
try {
long start = System.currentTimeMillis();
InputStream is = new FileInputStream(”conf.properties”);
Properties p = new Properties();
p.load(is);
is.close();
System.out.println(”SIZE : ” + p.size());
System.out.println(”homepage : ” + p.getProperty(”homepage”));
System.out.println(”author : ” + p.getProperty(”author”));
System.out.println(”school : ” + p.getProperty(”school”));
System.out.println(”date : ” + p.getProperty(”date”));
long end = System.currentTimeMillis();
System.out.println(”Cost : ” + (end – start));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * 实现properties文件的读取 * @author bbflyerwww * @date 2006-08-02 */ public class PTest { public static void main(String[] args) { try { long start = System.currentTimeMillis(); InputStream is = new FileInputStream(”conf.properties”); Properties p = new Properties(); p.load(is); is.close(); System.out.println(”SIZE : ” + p.size()); System.out.println(”homepage : ” + p.getProperty(”homepage”)); System.out.println(”author : ” + p.getProperty(”author”)); System.out.println(”school : ” + p.getProperty(”school”)); System.out.println(”date : ” + p.getProperty(”date”)); long end = System.currentTimeMillis(); System.out.println(”Cost : ” + (end – start)); } catch (IOException ioe) { ioe.printStackTrace(); } } }
conf.properties
Java代码
# Configuration fileauthor = bbflyerwww
school = WuHan University
date = 2006-08-02
# Configuration fileauthor = bbflyerwww school = WuHan University date = 2006-08-02
Result
SIZE:4
author : bbflyerwww
school : WuHan University
date : 2006-08-02
Cost : 0
posted on 2011-01-23 23:36 baby-fly 阅读(726) 评论(1) 编辑 收藏 引用 所属分类: Java
<!– </rdf:RDF> –>
Feedback
# re: Java properties使用各位指点一下 程序运行结果为找不到指定文件 2011-03-17 16:04 冰原植被
import java.io.FileInputStream;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileInputStream propFile=new FileInputStream(”myProperties.txt”);
Properties p=new Properties(System.getProperties());
p.load(propFile);
System.setProperties(p);
System.getProperties()。list(System.out);
}
}
[1][2]
冬天已经到来,春天还会远吗?