Properties文件是Java的一种配置文件,文件配置很简单,格式为`配置项=配置值`,我们可以直接通过`Properties`类来将其读取为一个类似于Map一样的对象
- public static void main(String[] args) throws IOException {
- Properties properties = new Properties();
- properties.load(new FileInputStream("test.properties"));
- System.out.println(properties);
- }
`Properties`类是继承自`Hashtable`,而`Hashtable`是实现的Map接口,也就是说,`Properties`本质上就是一个Map一样的结构,它会把所有的配置项映射为一个Map,这样就可以快速地读取对应配置的值了,也可以将已经存在的Properties对象放入输出流进行保存
- public static void main(String[] args) throws IOException {
- Properties properties = new Properties();
- // properties.setProperty("test", "t"); //和put效果一样
- properties.put("test", "t");
- properties.store(System.out, "????");
- //properties.storeToXML(System.out, "????"); 保存为XML格式
- }