1、生成 .xml 格式的配置文件:
1.1 生成配置文件
Properties properties = new Properties();//生成实例
properties.setProperty("qq", "com.tarena.factory.QQ");//设置键值 key----value
properties.storeToXML(new FileOutputStream("tarena.xml"), "k---v");//像新文件存储
1.2 加载配置文件
Properties properties2 = new Properties();
properties2.loadFromXML(new FileInputStream("tarena.xml"));
2、生成 .properties 格式的配置文件:
2.1 生成配置文件
Properties properties = new Properties();//生成实例
properties.setProperty("qq", "com.tarena.factory.QQ");//设置键值 key----value
properties.store(new FileOutputStream("tarena.properties"), "key===value");2.2 加载配置文件
Properties properties2 = new Properties();
roperties2.load(new FileInputStream("tarena.properties"));
本文介绍了如何使用Java的Properties类生成和加载两种不同格式的配置文件:.xml和.properties。详细展示了从创建Properties实例到设置键值对并保存为文件的过程,同时也提供了加载这些配置文件的方法。

1889

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



