Properties 继承了HashTable,是一个map类集合
可以读取 .properties 类型的文件,文件内容格式为 key = value
利用低级流进行读取(有点像高级流)
package properties; import java.io.FileReader; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; public class PropertiesTest { public static void main(String[] args){ Properties p = new Properties(); try { System.out.println("--------------------读取key对应的value----------------------"); p.load(new FileReader("src//properties//Test.properties")); String s = p.getProperty("key1"); //默认是String,会返回key = 后面的东西,不用加""等 System.out.println(s); //aaa System.out.println("--------------------返回所有key遍历----------------------"); @SuppressWarnings("unchecked") Enumeration e = p.propertyNames(); while(e.hasMoreElements()){ String key = (String)e.nextElement(); System.out.println(key + " = " + p.getProperty(key)); } //key3 = ccc //key2 = bbb //key1 = aaa //因为是map所以无序,返回顺序与存储顺序不一致 } catch (IOException e) { e.printStackTrace(); } } }
Test.properties 路径: src/properties/Test.properties
key1 = aaa key2 = bbb key3 = ccc