java--IO--Properties类
2021/6/19 17:30:33
本文主要是介绍java--IO--Properties类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- Properties的介绍
-
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("保存/创建成功"); } }
-
这篇关于java--IO--Properties类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)