import java.io.BufferedInputStream;
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.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UtilsProperties {
private static Logger log = LoggerFactory.getLogger(UtilsProperties.class);
/**
* 根据文件路径,key值读取对应value
* @param path
* @param key
* @return
*/
public static String readProperties(String path,String key){
long mStart = System.currentTimeMillis();
Properties prop = new Properties();
try {
InputStream is = new BufferedInputStream(new FileInputStream(path));
try {
prop.load(is);
long mEnd = System.currentTimeMillis();
log.info("文件读取成功,共耗时:{}",mEnd-mStart);
return prop.getProperty(key);
} catch (IOException e) {
log.debug("文件已获取,但读取异常");
e.printStackTrace();
log.debug("获取文件异常,可能该文件不存在");
e.printStackTrace();
}
return null;
}
/**
* 根据文件路径,获取配置文件所有键值对
* @param path
* @return
*/
public static Map<String,String> readAllProperties(String path){
Map<String,String> map = new HashMap<String,String>();
Properties prop = new Properties();
try {
InputStream is = new BufferedInputStream(new FileInputStream(path));
try {
prop.load(is);
Enumeration<?> en = prop.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = prop.getProperty(key);
map.put(key, value);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return map;
}
/**
* 将key和value写入指定配置文件
* @param path
* @param key
* @param value
*/
public static void writeProperties(String path, String key, String value){
Properties prop = new Properties();
try {
InputStream is = new BufferedInputStream(new FileInputStream(path));
try {
prop.load(is);
OutputStream os = new FileOutputStream(path);
prop.setProperty(key, value);
prop.store(os, "Update " + key + " name");
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//String value = UtilsProperties.readProperties("Test.properties","long");
//UtilsProperties.readAllProperties("Test.properties");
UtilsProperties.writeProperties("Test.properties","hahah","bbb");
}
}
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.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UtilsProperties {
private static Logger log = LoggerFactory.getLogger(UtilsProperties.class);
/**
* 根据文件路径,key值读取对应value
* @param path
* @param key
* @return
*/
public static String readProperties(String path,String key){
long mStart = System.currentTimeMillis();
Properties prop = new Properties();
try {
InputStream is = new BufferedInputStream(new FileInputStream(path));
try {
prop.load(is);
long mEnd = System.currentTimeMillis();
log.info("文件读取成功,共耗时:{}",mEnd-mStart);
return prop.getProperty(key);
} catch (IOException e) {
log.debug("文件已获取,但读取异常");
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据文件路径,获取配置文件所有键值对
* @param path
* @return
*/
public static Map<String,String> readAllProperties(String path){
Map<String,String> map = new HashMap<String,String>();
Properties prop = new Properties();
try {
InputStream is = new BufferedInputStream(new FileInputStream(path));
try {
prop.load(is);
Enumeration<?> en = prop.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = prop.getProperty(key);
map.put(key, value);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return map;
}
/**
* 将key和value写入指定配置文件
* @param path
* @param key
* @param value
*/
public static void writeProperties(String path, String key, String value){
Properties prop = new Properties();
try {
InputStream is = new BufferedInputStream(new FileInputStream(path));
try {
prop.load(is);
OutputStream os = new FileOutputStream(path);
prop.setProperty(key, value);
prop.store(os, "Update " + key + " name");
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//String value = UtilsProperties.readProperties("Test.properties","long");
//UtilsProperties.readAllProperties("Test.properties");
UtilsProperties.writeProperties("Test.properties","hahah","bbb");
}
}