作为一个屌丝程序员不得不收藏的工具类 一 基于spring的属性文件工具类

package cn.ipanel.ad.framework.util;

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.io.OutputStream;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
 * 属性文件工具类
 * @author lsm
 *
 */
public class PropertyUtil {
	
	private final static String fileName = "config.properties";
	
	/**
	 * 获取config配置文件配置,根据key获取值
	 * @param key
	 * @return
	 */
	public static String getPropertyValue(String key){
		try {
			File file = new File(CommonUtil.getAbsPathOfProject()+"WEB-INF/classes/config.properties");
			FileInputStream fin = new FileInputStream(file);
			Properties p = new Properties();
			p.load(fin);
			String value=p.getProperty(key).trim();
			fin.close();
			return value;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据属性关键字读取配置文件中的相关信息
	 * 如果fileName为null则读取config.properties的配置
	 * @since 
	 * @param filename 配置文件名
	 * @param key 属性关键字
	 * @return
	 */
	public static String getPropertyValue(String filename,String key){
		try {
			if(filename == null)
				filename = fileName;
			Resource  resource=new ClassPathResource(filename);
			InputStream inputStream =resource.getInputStream();
			Properties p = new Properties();
			p.load(inputStream);
			String value=p.getProperty(key);
			inputStream.close();
			return value;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 更改filename属性文件关键字的值
	 * @param filename
	 * @param key
	 * @param value
	 */
	public static void setPropertyValue(String filename,String key,String value){
		Properties prop = new Properties();
		try {
			Resource  resource=new ClassPathResource(filename);
			InputStream fis = resource.getInputStream();     
			prop.load(fis);
			File resourceFile=resource.getFile();
			String filepath=resourceFile.getPath();
			OutputStream fos = new FileOutputStream(filepath);    
			prop.setProperty(key, value);
			prop.store(fos, "Update '" + key + "' value");    
		} catch (IOException e) {     
			e.printStackTrace();    
		}
	}
	/**
	 * 获取filename配置文件配置,根据key获取值
	 * @param filename
	 * @param key
	 * @return
	 */
	public static String getPropertyValueNoCache(String filename,String key){
		try {
			Resource  resource=new ClassPathResource(filename);
			File file=resource.getFile();
			InputStream inputStream=new FileInputStream(file.getAbsolutePath());
			Properties p = new Properties();
			p.load(inputStream);
			String value=p.getProperty(key);
			inputStream.close();
			return value;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值