package cn.four; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; import java.util.Set; /** *主要介绍Properties类 * 这个类是线程安全的, 多个类可以共享一个Properties对象, 而不需要外部同步 * Properties表示一组持久的属性,Properties可以保存到流中或从流中加载 * Properties extends Hashtable * 强烈建议使用setProperty()方法插入键值对,因为这样键和值都是String类型 * String getProperty(String key) * Set<String> stringPropertyNames() 获取一组键的set集合 *void load(Reader reader) 从流中加载 *void load(InputStream inStream) 从流中加载 * // 将此属性列表以适当的流写入输出流, comment表示描述信息, 可设为空字符串 * void store(OutputStream out, String comments) * void store(Writer writer, String comments) */ public class OperationsOfProperties { public static void main(String[] args) throws Exception { Properties proterty = new Properties(); proterty.setProperty("周瑜", "小乔"); proterty.setProperty("孙策", "大桥"); proterty.setProperty("吕布", "貂蝉"); // 用store将键值对以流的方式存入文件 proterty.store(new FileWriter("G:\\demoTest\\a.properties"), " "); // 创建第二个对象,用于加载数据 Properties proterty2 = new Properties(); // 加载 proterty2.load(new FileReader("G:\\demoTest\\a.properties")); Set<String> set = proterty2.stringPropertyNames(); for (String s : set) { System.out.print(s + "=" + proterty2.getProperty(s) + " "); } // 孙策=大桥 吕布=貂蝉 周瑜=小乔 } }
Properties
最新推荐文章于 2024-01-30 08:00:00 发布