标签:not tab 配置文件信息 point pass reader val close hashcode
Properties应用案例:
package com.model.io.properties; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/19 14:09 */ public class PropertiesDemo01 { //传统的方法读取properties文件 public static void main(String[] args) throws IOException { String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Properties\\PropertiesDemo01.properties"; BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath)); String readLine=null; while((readLine=bufferedReader.readLine())!=null){ String[] split = readLine.split("="); System.out.println(split[0]+"的值是:"+split[1]); } bufferedReader.close(); } }
package com.model.io.properties; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; import java.util.Properties; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/19 14:00 * * 使用Properties类读取配置文件信息 */ public class PropertiesDemo02 { public static void main(String[] args) throws IOException { String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Properties\\PropertiesDemo01.properties"; String filePath1="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Properties\\PropertiesDemo02.properties"; //创建properties对象 Properties properties = new Properties(); //加载指定的配置 文件 properties.load(new FileInputStream(filePath)); //修改打印位置控制台。并打印所有的键值对 properties.list(System.out); //修改打印位置为一个文件,将配置文件的信息都打印到其中 properties.list(new PrintStream(filePath1)); //getProperties获取某一个值 System.out.println(properties.getProperty("password")); System.out.println(properties.getProperty("username")); } }
package com.model.io.properties; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/19 14:27 * * 使用Properties类 修改创建配置文件 */ public class PropertiesDemo03 { public static void main(String[] args) throws IOException { //创建的配置文件的位置 String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Properties\\PropertiesDemo03.properties"; /* * Properties类的父类是Hashtable,底层核心代码就是Hashtable核心方法 * public synchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> entry = (Entry<K,V>)tab[index]; for(; entry != null ; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) { V old = entry.value; entry.value = value; return old; } } addEntry(hash, key, value, index); return null; } * */ Properties properties = new Properties(); //如果文件中存在key。则是替换修改,如果key不存在则是创建新的key-value properties.setProperty("username", "root"); properties.setProperty("password", "123456"); properties.setProperty("name", "张三"); //他在配置文件中不会保存汉字,而是保存汉字对应的unicode码 properties.store(new FileWriter(filePath), null); System.out.println("保存/创建成功"); } }
标签:not tab 配置文件信息 point pass reader val close hashcode
原文地址:https://www.cnblogs.com/zzhAylm/p/14902904.html