- java.util包
2. 继承 HashTable(HashMap的早期版本) 所以使用方式像是map集合
3. 虽然从继承关系来讲Proterties使用方式像是map集合 但从读取的东西来说 它读取的是文件中的信息 所以它更像是一个流(高级流)
4. 构造方法 一个字节型输入流 一个字符型输入流
Properties pro = new Properties();
pro.load(new FileInputStream(“src//Test.properties”)); //字节型
pro.load(new FileReader(“src//Test.properties”)); //字符型
更常用的方法(通常文件的路径是未知的 所以可以这样写)
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(“Test.properties”);
properties.load(is);
5. 读取的文件后缀名 需是 .propertied 形式 文件中的内容 以key=value形式存在 格式不能改 但是=可以改 可以用 : 或者 空格public static void main(String[] args) {try {Properties pro = new Properties();//构造方法有两个 一个字节型 一个字符型//字节型// pro.load(new FileInputStream("src//Test.properties"));//字符型pro.load(new FileReader("src//Test.properties"));//读取的文件后缀名 需是 .propertied 形式//文件中的内容 以key=value形式存在的 格式不能改 但是=可以改 可以用 : 或者 空格//获取key1键对应的value值String value = pro.getProperty("key1");//获取全部的键 用于遍历 类似于Set = map.keySet();Enumeration en = pro.propertyNames();// Enumeration对象的用法类似于迭代器 en.hasMoreElements(); en.nextElement();// Iterator it.hasNext(); it.next();while(en.hasMoreElements()){//这里需要造型 之前的Iterator是需要泛型 但是这里泛型会报错String key = (String)en.nextElement();String value1 = pro.getProperty(key);System.out.println(value1);}} catch (IOException e) {e.printStackTrace();}}
