Java教程 propertiesl 的读写问题

properties 的 读写问题:
关于properties 的读写问题的相关描述:
1、了解其中含义
package com.situ.chapter3; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.Properties; import java.util.Set; public class Test1 { public static void main(String[] args) throws IOException { // 文件分为相对路径和绝对路径。绝对路径缺点:不灵活。 // File file = new File("D:\\workspace\\lession0324\\src\\com\\situ\\chapter3/config.properties"); // System.out.println(file.exists()); // System.out.println(file.getName()); // System.out.println(file.getAbsolutePath()); // InputStream is = Thread.currentThread().getContextClassLoader() // .getResourceAsStream("com/situ/chapter3/config.properties"); InputStream is = Test1.class.getResourceAsStream("config.properties"); // System.out.println(is == null); // 类,用于读写properties文件类 Properties properties = new Properties(); OutputStream os = null; try { // 加载properties文件内容 properties.load(is); // 返回所有的key Set<Object> keys = properties.keySet(); for (Object key : keys) { System.out.println(key.toString() + "=" + properties.getProperty(key.toString())); } // 设置属性值 properties.setProperty("birthday", "1999-10-10"); // 进行保存 // File f = new File("d:/abc.properties"); URL url = Test1.class.getResource("config.properties"); os = new FileOutputStream(new File(url.getPath())); properties.store(os, "我的只在"); os.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { os.close(); } } } }
作者为:刘广法,网站地址:https://liuguangfa.com/