中国建设工程造价管理协会网站查询,快速网站开发,软件应用商店下载安装,开发公司终止函内容Java 对 Properties 文件的操作详解及简单实例发布于 2020-8-7|复制链接摘记: Java 对 Properties 文件的操作简介在 Java 中#xff0c;我们常用 java.util.Properties.Properties 类来解析 Properties 文件#xff0c;Properties 格式文件是 Java 常用的配置文件#xff0…Java 对 Properties 文件的操作详解及简单实例发布于 2020-8-7|复制链接摘记: Java 对 Properties 文件的操作简介在 Java 中我们常用 java.util.Properties.Properties 类来解析 Properties 文件Properties 格式文件是 Java 常用的配置文件它用来在文件中存储键-值对其中键和值用等号分隔格 ..Java 对 Properties 文件的操作简介在 Java 中我们常用 java.util.Properties.Properties 类来解析 Properties 文件Properties 格式文件是 Java 常用的配置文件它用来在文件中存储键-值对其中键和值用等号分隔格式如下javanameshawearnProperties 类是 java.util.Hashtable 的子类用于键和值之间的映射。在对 Properties 格式文件的操作中我们常使用 Properties 类的一下方法Properties()用于创建一个无任何属性值 Properties 对象void load(InputStream inStream)从输入流中加载属性列表void store(OutputStream out, String comments)根据输出流将属性列表保存到文件中String getProperty(String key)获取指定键的值void setProperty(String key, String value)设置指定键的值若指定键已经在原属性值列表中存在则覆盖若指定键在原属性值列表中不存在则新增写入 Properties 文件java// 创建一个 Properties 实例Properties p new Properties();// 为 Properties 设置属性及属性值p.setProperty(name, shawearn);p.setProperty(address, XX 省 XX 市);// 保存 Properties 到 shawearn.properties 文件中FileOutputStream out new FileOutputStream(shawearn.properties);p.store(out, Create by Shawearn!);out.close();读取 Properties 文件java// 创建一个 Properties 实例Properties p new Properties();// 读取配置文件FileInputStream in new FileInputStream(shawearn.properties);// 加载配置文件到 Properties 实例中p.load(in);in.close();最后附上测试代码javapackage com.shawearn.test;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;import java.util.Set;/*** author Shawearn**/public class TestProperties {/*** param args* throws IOException*/public static void main(String[] args) throws IOException {TestProperties t new TestProperties();// 测试写入t.testWrite();// 测试读取t.testRead();}/** 测试对 Properties 文件的写入操作*/private void testWrite() throws IOException {// 创建一个 Properties 实例Properties p new Properties();// 为 Properties 设置属性及属性值p.setProperty(name, shawearn);p.setProperty(address, XX 省 XX 市);// 保存 Properties 到 shawearn.properties 文件中FileOutputStream out new FileOutputStream(shawearn.properties);p.store(out, Create by Shawearn!);out.close();System.out.println(写入成功);}/** 测试对 Properties 文件的读取操作*/private void testRead() throws IOException {// 创建一个 Properties 实例Properties p new Properties();// 读取配置文件FileInputStream in new FileInputStream(shawearn.properties);// 加载配置文件到 Properties 实例中p.load(in);in.close();// 获取 Properties 文件中所有的 keySet keys p.stringPropertyNames();// 遍历所有的 keyfor (String key : keys) {// 获取 Properties 文件中 key 所对应的 valueObject value p.get(key);// 输入 key 和对应的 valueSystem.out.println(key value);}}}控制台输出结果javaaddress XX 省 XX 市name shawearnshawearn.properties 文件内容java#Create by Shawearn!#Thu Nov 19 12:43:41 CST 2015nameshawearnaddressXX \u7701 XX \u5E02