`

properties配置文件的操作

阅读更多
package com.csair.rsa;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 对properties的各种操作
 * 
 * @author Administrator
 * @data Mar 2, 2012
 */
public class PropertiesBean {
	private String fileUrl;// Properties配置文件的路径
	private Properties properties = null;
	private FileInputStream fileInputStream = null;
	private FileOutputStream fileOutputStream = null;

	/**
	 * 构造函数
	 */
	public PropertiesBean() {
	}

	/**
	 * 初始化配置文件
	 * 
	 * @param fileUrl
	 */
	public void initData() {
		properties = new Properties();
		try {
			fileInputStream = new FileInputStream(fileUrl);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.err.println("找不到相关的配置文件~~~");
			e.printStackTrace();
		}
		try {
			properties.load(fileInputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.err.println("加载配置文件失败~~~");
			e.printStackTrace();
		}
		if (fileInputStream != null) {
			try {
				fileInputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.err.println("关闭文件失败~~~");
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取配置文件对应字段的数据
	 * 
	 * @param itemName
	 * @return
	 */
	public String getValue(String itemName) {
		return properties.getProperty(itemName);
	}

	/**
	 * 获取配置文件相关的字段数据,如果查询不到返回默认值defaultValue
	 * 
	 * @param itemName
	 * @param defaultValue
	 * @return
	 */
	public String getValue(String itemName, String defaultValue) {
		return properties.getProperty(itemName, defaultValue);
	}

	/**
	 * 设置配置文件对应的参数值,设置完以后要保存文件(执行saveFile方法)
	 * 
	 * @param itemName
	 * @param value
	 */
	public void setValue(String itemName, String value) {
		properties.setProperty(itemName, value);
	}

	/**
	 * 删除配置文件中对应的参数值,设置完以后要保存文件(执行saveFile方法)
	 * 
	 * @param value
	 */
	public void deleteValue(String value) {
		properties.remove(value);
	}

	/**
	 * 保存到配置文件中
	 * 
	 * @param filename
	 * @param description
	 * @throws Exception
	 */
	public void saveFile(String filename, String description) throws Exception {
		try {
			File f = new File(filename);
			fileOutputStream = new FileOutputStream(f);
			properties.store(fileOutputStream, description);
			fileOutputStream.close();
		} catch (IOException ex) {
			throw new Exception("无法保存指定的配置文件:" + filename);
		}
	}

	/**
	 * 获取配置文件的路径(此方法是放置到src的目录下,如果是其他路径下,修改对应的地址就可以了。)
	 * 
	 * @param fileName
	 * @return
	 */
	public String getPath(String fileName) {
		return getClass().getClassLoader().getResource("").getPath() + fileName;
	}

	/**
	 * 主方法测试类
	 * @param args
	 */
	public static void main(String[] args) {
		PropertiesBean pu = new PropertiesBean();
		pu.fileUrl = pu.getPath("key.properties");
		pu.initData();
//		pu.deleteValue("sss");
//		try {
//			pu.saveFile(pu.fileUrl, "just Test");
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		String publicKey = pu.getValue("sss");
		System.out.println(publicKey);

	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics