该类专门用于读写配置文件的集合类
配置文件的格式为:
键=值
键=值
键值对不需要有空格,值不需要用引号""包裹,默认的类型是String
案例:
username=admin password=123456
@Test public void readProperties1() throws IOException { Properties properties = new Properties(); properties.load(new FileReader("src\\main\\resources\\application.properties")); System.out.println("--------显示所有键值对--------"); properties.list(System.out); System.out.println("-----根据key获取value的值-----"); System.out.println("username=" + properties.getProperty("username")); System.out.println("password=" + properties.getProperty("password")); System.out.println("---------修改value值---------"); properties.setProperty("password", "111111"); System.out.println("password=" + properties.getProperty("password")); System.out.println("----------新建键值对----------"); properties.setProperty("charset", "gbk"); System.out.println("charset=" + properties.getProperty("charset")); System.out.println("---------保存配置文件---------"); properties.store(new FileWriter("src\\main\\resources\\application.properties"), null); System.out.println("保存成功"); }
#Wed Jul 21 13:55:45 CST 2021 password=111111 charset=gbk username=admin