使用Properties集合存储数据,遍历取出Properties集合中的数据与Properties集合中的方法store
使用Properties集合存储数据,遍历取出Properties集合中的数据
概述
java.util. Properties继承于Hashtable,来表示一个持久的属性集。它使用键值结构存储数据,每对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,system.getProperties方法一个Properties对象。
Properties类构造方法
public Properties(:创建一个空的属性列表。
基本的存储方法
public 0bject setProperty (String key,string value):保存一对属性。
public string getProperty(String key):使用此属性列表中指定的键搜索属性值。public Set<String> stringPropertyNames()︰所有键的名称的集合。
Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
package Demo_Properties; import java.util.Properties; import java.util.Set; /* java.util.Properties集合extends Hashtable<k, v> implements Map<k, v>Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。Properties集合是一个唯—和i0流相结合的集合 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储可可以使用Properties集合中的方法Load,把硬盘中保存的文件(键值对),读取到集合中使用 属性列表中每个键及其对应值都是一个字符串。 Properties集合是一个双列集合, key和value黑认都是字符串 */ public class Demo01_Properties { public static void main(String[] args) { show01(); } /* 使用Properties集合存储数据,遍历取出Properties集合中的数据 Properties集合是一个双列集合,key和value黑认都是字符串 Properties集合有一些操作字符串的特有方法 object setProperty (String key,String value)调用Hashtable 的方法 put。 String getProperty (String key)通过key找到value值,此方法相当于MNap集合中的get(key)方法 Set<String stringPropertyNames()返回此属性列表中的键集,其中该键及其对应值是字符串,,此方法相当于MNap集合中的keyset方法 */ private static void show01() { //创建 Properties集合对象 Properties pes = new Properties(); //使用setProperty往集合中添加数据 pes.setProperty("马可波罗","152"); pes.setProperty("安琪拉","106"); pes.setProperty("鲁班大师","103"); //pes.put(1,true); //使用stringPropertyNames把Properties集合中的键取出,存储到一个set集合中 Set<String> set = pes.stringPropertyNames(); //遍历Set集合,取出Properties 集合的每一个键 for (String key : set){ String value = pes.getProperty(key); System.out.println(key+"="+value); } } }
Properties集合中的方法store
package Demo_Properties; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; import java.util.Set; /* java.util.Properties集合extends Hashtable<k, v> implements Map<k, v>Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。Properties集合是一个唯—和i0流相结合的集合 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储可可以使用Properties集合中的方法Load,把硬盘中保存的文件(键值对),读取到集合中使用 属性列表中每个键及其对应值都是一个字符串。 Properties集合是一个双列集合, key和value黑认都是字符串 */ public class Demo01_Properties { public static void main(String[] args) throws IOException { show02(); } /* 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储void store( outputstream out,string comments ) void store (luriter writer,string comments)参数: OutputStream out :字节输出流,不能写入中文writer writer:字符输出流,可以写中文 String comments :注释,用来解释说明保存的文件是做什么用的 不能使用中文,会产生乱码,黑认是unicode编码 一般使用""空字符串 使用步骤: 1.创建Properties集合对象,i添加数据 2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地 3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 4.释放资源 */ private static void show02() throws IOException { Properties pes = new Properties(); //使用setProperty往集合中添加数据 pes.setProperty("马可波罗","152"); pes.setProperty("安琪拉","106"); pes.setProperty("鲁班大师","103"); /* //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地 FileWriter fileWriter = new FileWriter("q.txt"); //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 pes.store(fileWriter,"save data"); //4.释放资源 fileWriter.close(); */ pes.store(new FileOutputStream("q.txt"),""); } /* 使用Properties集合存储数据,遍历取出Properties集合中的数据 Properties集合是一个双列集合,key和value黑认都是字符串 Properties集合有一些操作字符串的特有方法 object setProperty (String key,String value)调用Hashtable 的方法 put。 String getProperty (String key)通过key找到value值,此方法相当于MNap集合中的get(key)方法 Set<String stringPropertyNames()返回此属性列表中的键集,其中该键及其对应值是字符串,,此方法相当于MNap集合中的keyset方法 */ private static void show01() { //创建 Properties集合对象 Properties pes = new Properties(); //使用setProperty往集合中添加数据 pes.setProperty("马可波罗","152"); pes.setProperty("安琪拉","106"); pes.setProperty("鲁班大师","103"); //pes.put(1,true); //使用stringPropertyNames把Properties集合中的键取出,存储到一个set集合中 Set<String> set = pes.stringPropertyNames(); //遍历Set集合,取出Properties 集合的每一个键 for (String key : set){ String value = pes.getProperty(key); System.out.println(key+"="+value); } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Blazor Hybrid适配到HarmonyOS系统
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库