java.util.Properties
类,那么如今告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的,如清单 1 所示。
Properties
对象中后,您就能够找到两个键( foo
和 fu
)和两个值( foo
的 bar
和 fu
的 baz
)了。
InputStream
给 load()
方法,就会将每个键-值对添加到 Properties
实例中。而后用 list()
列出全部属性或者用 getProperty()
获取单独的属性。
import java.util.*; import java.io.*;public class LoadSample { public static void main(String args[]) throws Exception { Properties prop = new Properties(); FileInputStream fis =new FileInputStream("sample.properties"); prop.load(fis); prop.list(System.out); System.out.println("\nThe foo property: " +prop.getProperty("foo")); } } |
list()
方法的输出中键-值对的顺序与它们在输入文件中的顺序不同。 Properties
类在一个散列表(hashtable,事实上是一个 Hashtable
子类)中储存一组键-值对,因此不能保证顺序。
java.util.Properties
类如今提供了一种为程序装载和存储设置的更容易的方法: loadFromXML(InputStream is)
和 storeToXML(OutputStream os, String comment)
方法。
<?xml version="1.0" encoding="UTF-8"?> <!-- DTD for properties --> <!ELEMENT properties ( comment?, entry* ) > <!ATTLIST properties version CDATA #FIXED "1.0"> <!ELEMENT comment (#PCDATA) > <!ELEMENT entry (#PCDATA) > <!ATTLIST entry key CDATA #REQUIRED> |
<properties>
标签中包装的是一个 <comment>
标签,后面是任意数量的 <entry>
标签。对每个 <entry>
标签,有一个键属性,输入的内容就是它的值。清单 5 显示了
清单 1中的属性文件的 XML 版本是什么样子的。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Hi</comment> <entry key="foo">bar</entry> <entry key="fu">baz</entry> </properties> |
Properties
文件与读取老格式的文件没什么不一样。
import java.util.*; import java.io.*; public class LoadSampleXML { public static void main(String args[]) throws Exception { Properties prop = new Properties(); FileInputStream fis =new FileInputStream("sampleprops.xml"); prop.loadFromXML(fis); prop.list(System.out); System.out.println("\nThe foo property: " +prop.getProperty("foo")); } } |
Properties
还有一个功能是将属性存储到 XML 格式的文件中。虽然 store()
方法仍然会建立一个相似
清单 1 所示的文件,可是如今能够用新的
storeToXML()
方法建立如
清单 5 所示的文件。只要传递一个
OutputStream
和一个用于注释的 String
就能够了。清单 7 展现了新的 storeToXML()
方法。
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(); } } |
Properties
还有一个功能是将属性存储到 XML 格式的文件中。虽然 store()
方法仍然会建立一个相似
清单 1 所示的文件,可是如今能够用新的
storeToXML()
方法建立如
清单 5 所示的文件。只要传递一个
OutputStream
和一个用于注释的 String
就能够了。清单 7 展现了新的 storeToXML()
方法。
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(); } } |
<?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> |
Properties
对象。