您的位置:首页 > 编程语言 > Java开发

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: