java properties读取 封装_Java properties 文件读取 工具类的封装

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class PropertyUtil {

private static Log log = LogFactory.getLog(PropertyUtil.class);

private static String PATH = "config//sftp.properties";

public static void setPATH(String pATH) {

PATH = pATH;

}

public static String getValue(String key) {

Properties props = new Properties();

try {

InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(PATH);//new BufferedInputStream(new FileInputStream(PATH));

props.load(in);

String value = props.getProperty(key);

return value;

} catch (Exception e) {

e.printStackTrace();

log.error(e.getMessage());

return null;

}

}

public static Map getValue( Map map) {

Properties props = new Properties();

try {

InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(PATH);//new BufferedInputStream(new FileInputStream(PATH));

props.load(in);

for (Map.Entry entry : map.entrySet()) {

String key=entry.getKey();

String value = props.getProperty(key);

map.put(key, value);

}

return map;

} catch (Exception e) {

e.printStackTrace();

log.error(e.getMessage());

return null;

}

}

public static void setValue(String key, String value) {

Properties prop = new Properties();

try {

InputStream fis = PropertyUtil.class.getClassLoader().getResourceAsStream(PATH);

prop.load(fis);

OutputStream fos = new FileOutputStream(PropertyUtil.class.getClassLoader().getResource("").getPath() + PATH);

prop.setProperty(key, value);

prop.store(fos, "Update '" + key + "' value");

} catch (IOException e) {

log.error("Visit " + PATH + " for updating " + key + " value error");

}

}

public static void main(String[] args) {

Map map = new HashMap();

map.put("server", "");

map.put("user", "");

map.put("password", "");

map=PropertyUtil.getValue(map);

System.out.println(map.get("server"));

System.out.println(map.get("user"));

System.out.println(map.get("password"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值