不经意看到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
本文介绍了 Java 中 Properties 类的使用方法,包括如何从 XML 文件中加载属性,并提供了具体的代码示例。此外,还强调了 XML 文件格式的要求。
4155

被折叠的 条评论
为什么被折叠?



