1 2 | Properties(): 创建一个Properties集合 Properties pro=new Properties(); |
1 2 3 4 5 6 7 8 9 | Object setProperty(String key, String value): 保存/替换键值对 pro.setProperty(String key, String value); String getProperty(String key): 通过键获取值. 键不存在返回null pro.getProperty(String key) Set<String> stringPropertyNames(): 返回键的集合 Set<String> str=pro.pro.stringPropertyNames() void store(OutputStream out, String comments): 将集合数据用字节流写入文件(不能中文), 并写入注释 void store(Writer writer, String comments): 将集合数据用字符流写入文件(可以中文), 并写入注释 pro.store(new FileWriter("F:\\IdeaProjects\\prop.properties"),""); |
1 2 3 | void load(InputStream inStream): 从配置文件中通过字节流加载数据到Properties集合(不能读中文) void load(Reader reader): 从配置文件中通过字符流加载数据到Properties集合(可以读中文) pro.load(new FileReader("F:\\IdeaProjects\\prop.properties")); |
01 02 03 04 05 06 07 08 09 10 11 12 13 | public class Test { public static void main(String[] args) throws IOException { // 创建Properties对象 Properties properties = new Properties(); // 调用load()方法读取数据 properties.load(new FileReader("day09\\prop.properties")); // 此时properties集合中已经有了键值对, 遍历 Set<String> set = properties.stringPropertyNames(); for (String key : set) { // 通过键获取值 String value = properties.getProperty(key); System.out.println(key + "=" + value); }}} |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |