Java简单操作Properties配置文件(实例详解)
2017-05-29 14:48
851 查看
目录
目录工具类
方法说明
源代码
应用实例
新建文件
读取记录
修改新增
工具类
方法说明
方法名 | 功能 |
---|---|
PropertiesHelper() | 构造方法 |
PropertiesHelper(String fileName) | 构造方法(配置文件名) |
String getValue(String key) | 通过键获取值 |
void setValue(String key,String value) | 新增或修改记录 |
void clear() | 清空操作 |
boolean exist() | 判断默认文件是否存在 |
boolean exist(String fileName) | 判断指定文件是否存在 |
void readFile() | 读取默认配置文件 |
readFile(String fileName) | 读取指定配置文件 |
void save() | 保存修改 |
void saveAs(String newFileName) | 将文件另存为指定文件名 |
源代码
类名:PropertiesHelper.java
添加引用:
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import java.util.Properties;
源码:
/** * properties配置文件读取助手 * @author landr * 修改时间:2017-5-29 11:01:00 */ public class PropertiesHelper { private HashMap<String, String> hashMap = new HashMap& 4000 lt;String, String>(); private String fileName; public PropertiesHelper() {} public PropertiesHelper(String fileName) { this.fileName = fileName; } /** * 通过键获取值 * @param key * @return 记录值 */ public String getValue(String key){ if(!hashMap.containsKey(key)) return null; return hashMap.get(key); } /** * 新增或修改记录 * @param key * @param value */ public void setValue(String key,String value){ hashMap.put(key, value); } /** * 清空操作 */ public void clear(){ hashMap.clear(); fileName = null; } /** * 判断默认文件是否存在 * @return */ public boolean exist(){ return new File(fileName).exists(); } /** * 判断指定文件是否存在 * @param fileName 文件路径 * @return */ public boolean exist(String fileName){ return new File(fileName).exists(); } /** * 读取默认配置文件 * @throws IOException */ public void readFile() throws IOException{ if(fileName==null){ throw new FileNotFoundException("Unspecified file name."); } readFile(fileName); } /** * 读取指定配置文件 * 默认配置文件会被修改为新指定的配置文件 * @param fileName 文件名(包含文件格式后缀) * @throws IOException 指定文件不存在/被占用 */ public void readFile(String fileName) throws IOException { Properties prop = new Properties(); this.fileName = fileName; hashMap.clear(); InputStream in = new BufferedInputStream(new FileInputStream(fileName)); prop.load(in); //加载属性列表 //遍历配置文件将数据转存至HashMap中 Iterator<String> it = prop.stringPropertyNames().iterator(); while (it.hasNext()) { String key = it.next(); hashMap.put(key, prop.getProperty(key)); } in.close(); } /** * 保存修改 * @throws FileNotFoundException * @throws IOException */ public void save() throws FileNotFoundException, IOException { Properties prop = new Properties(); if(fileName==null){ throw new FileNotFoundException("Unspecified file name."); } FileOutputStream fos = new FileOutputStream(fileName); //遍历HashMap Iterator<Entry<String, String>> iter = hashMap.entrySet().iterator(); while (iter.hasNext()) { Entry<String, String> entry = iter.next(); prop.setProperty(entry.getKey(), entry.getValue()); } //写入文件 prop.store(fos, null); fos.close(); } /** * 将文件另存为指定文件名 * @param newFileName * @throws IOException */ public void saveAs(String newFileName) throws IOException{ Properties prop = new Properties(); FileOutputStream fos = new FileOutputStream(newFileName,true); //遍历HashMap Iterator<Entry<String, String>> iter = hashMap.entrySet().iterator(); while (iter.hasNext()) { Entry<String, String> entry = iter.next(); prop.setProperty(entry.getKey(), entry.getValue()); } //写入文件 prop.store(fos, null); fos.close(); } }
应用实例
新建文件
String fileName = "test.properties"; PropertiesHelper ph = new PropertiesHelper(); ph.setValue("K1", "1"); ph.setValue("K2", "2"); ph.setValue("K3", "3"); ph.saveAs(fileName);
读取记录
String fileName = "test.properties"; PropertiesHelper ph = new PropertiesHelper(fileName); ph.readFile(); String str = ph.getValue("K1"); System.out.println(str);
修改/新增
String fileName = "test.properties"; PropertiesHelper ph = new PropertiesHelper(fileName); ph.setValue("K1", "abc"); //修改 ph.setValue("K4", "123"); //新增 ph.save();
相关文章推荐
- Java 对 Properties 文件的操作详解及简单实例
- Java 操作Properties配置文件详解
- java配置文件之xml和properties简单读取实例
- java中properties配置文件的操作
- 关于Java配置文件properties的使用(简单易用版)
- JAVA操作properties文件的代码实例
- Java配置文件Properties的读取、写入与更新操作
- java基础之对properties配置文件的读写操作
- java对properties配置文件的读写操作
- 对Java配置文件Properties的读取、写入与更新操作
- JAVA操作properties配置文件——总结(Locale&ResourceBundle& PropertyResourceBundle)
- java 属性文件(配置文件)的简单操作
- java.util.properties工具类操作properties配置文件
- Java配置文件Properties的读取、写入与更新操作
- Linux(centos 6.5) 调用java脚本以及定时执行的脚本实例及配置文件详解
- Java配置文件Properties的读取、写入与更新操作
- Java配置文件Properties的读取、写入与更新操作
- Java中properties文件的简单操作
- java properties配置文件操作
- JAVA随笔篇三(读写Properties配置文件以及路径问题详解)