Java简单操作Properties配置文件(实例详解)
来源:互联网 发布:网络研修日志 编辑:程序博客网 时间:2024/05/21 11:30
目录
- 目录
- 工具类
- 方法说明
- 源代码
- 应用实例
- 新建文件
- 读取记录
- 修改新增
工具类
方法说明
源代码
类名:
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<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();
阅读全文
1 0
- Java简单操作Properties配置文件(实例详解)
- properties配置文件操作实例
- java 操作配置文件 .properties
- java操作properties配置文件
- java配置文件之xml和properties简单读取实例
- Java中配置文件Properties类的简单操作
- JAVA操作properties文件实例
- java 中简单读取Properties配置文件
- java获取properties配置文件最简单方法
- Java对properties配置文件的操作
- java中properties配置文件的操作
- java对properties配置文件的读写操作
- java对properties配置文件的读写操作
- java.util.properties工具类操作properties配置文件
- JAVA操作properties文件的代码实例
- struts.properties配置文件详解
- log4j.properties配置文件详解
- quartz.properties 配置文件详解
- MySQL数据库覆盖索引
- 文档新建、打开及关闭的过程
- 千万千万不要运行的Linux命令
- 函数的公有属性和私有属性
- .NET内置特性(一)
- Java简单操作Properties配置文件(实例详解)
- hive原理与源码分析-ACID及总结(八)
- 用python写登陆界面
- AVL树的删除
- Filter | 日志处理 | 应用例子
- 接口和抽象类有什么区别
- 自定义-MVC
- 浅谈程序开发中的编程思想
- 【机器学习】人工神经网络(ANN)浅讲