不经意看到Properties 这个类,一直以为某个jar包里的,没发现是hashtable的子类,于是看了一下读取文件的方法。
读取xml:
Properties pro=new Properties();
pro.loadFromXML(new FileInputStream(new File("D:/projects/test/src/xmlread/root.xml")));
String root=pro.getProperty("root");
System.out.println(root);
pro.load(new FileInputStream(new File("D:/projects/test/src/xmlread/top.properties")));
String username=pro.getProperty("username");
System.out.println(username);
xml的格式必须符合一下格式,不是随便写节点就行的。
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>xml</comment> <entry key="root">test the properties</entry> </properties>
属性文件:
username=ssg