packagecn.xm.exam.utils;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.Properties;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/***
*@authorQiaoLiQiang
* @time 2018年11月3日下午12:05:32*/
public classPropertiesFileUtils {private static final Logger log = LoggerFactory.getLogger(PropertiesFileUtils.class);/*** 构造函数私有化*/
privatePropertiesFileUtils() {
}/*** 保存或更新properties文件中的key
*
*@paramfileName
*@paramkey
*@paramvalue*/
public static voidsaveOrUpdateProperty(String fileName, String key, String value) {
Properties properties= newProperties();
InputStream inputStream;
OutputStream outputStream;try{
String path= ResourcesUtil.class.getClassLoader().getResource(fileName).getPath();
log.debug("path -> {}", path);
inputStream= new FileInputStream(newFile(path));
properties.load(inputStream);
properties.setProperty(key, value);//保存到文件中(如果有的话会自动更新,没有会创建)
outputStream = new FileOutputStream(newFile(path));
properties.store(outputStream,"");
outputStream.close();
inputStream.close();
}catch(FileNotFoundException e) {
log.error("saveOrUpdateProperty error", e);
}catch(IOException e) {
log.error("saveOrUpdateProperty error", e);
}
}/*** 获取Properties
*
*@paramfileName
*@paramkey
*@return
*/
public staticString getPropertyValue(String fileName, String key) {
Properties properties= newProperties();
InputStream inputStream;
String value= "";try{
String path= PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath();
log.info("path -> {}", path);
inputStream= new FileInputStream(newFile(path));
properties.load(inputStream);
value=properties.getProperty(key);//保存到文件中(如果有的话会自动更新,没有会创建)
inputStream.close();
}catch(FileNotFoundException e) {
log.error("saveOrUpdateProperty error", e);
}catch(IOException e) {
log.error("saveOrUpdateProperty error", e);
}returnvalue;
}/*** 获取Properties
*
*@paramfileName
*@return
*/
public staticProperties getProperties(String fileName) {
Properties properties= newProperties();
InputStream inputStream;try{
String path= PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath();
log.info("path -> {}", path);
inputStream= new FileInputStream(newFile(path));
properties.load(inputStream);
inputStream.close();
}catch(FileNotFoundException e) {
log.error("saveOrUpdateProperty error", e);
}catch(IOException e) {
log.error("saveOrUpdateProperty error", e);
}returnproperties;
}/*** 获取Properties
*
*@paramfileName
*@return
*/
public staticProperties removeProperty(String fileName, String key) {
Properties properties= newProperties();
InputStream inputStream;
OutputStream outputStream;try{
String path= PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath();
log.info("path -> {}", path);
inputStream= new FileInputStream(newFile(path));
properties.load(inputStream);
log.info("properties -> {}", properties);if (properties != null &&properties.containsKey(key)) {
log.info("remove key:{}", key);
properties.remove(key);
}//保存到文件中(将properties保存到文件)
outputStream = new FileOutputStream(newFile(path));
properties.store(outputStream,"");
outputStream.close();
inputStream.close();
}catch(FileNotFoundException e) {
log.error("saveOrUpdateProperty error", e);
}catch(IOException e) {
log.error("saveOrUpdateProperty error", e);
}returnproperties;
}/*** 保存或更新properties文件中的key
*
*@parampath
* 文件全路径
*@paramkey
*@paramvalue*/
public static voidsaveOrUpdatePropertyByFilePath(String path, String key, String value) {
Properties properties= newProperties();
InputStream inputStream;
OutputStream outputStream;try{
log.debug("path -> {}", path);
inputStream= new FileInputStream(newFile(path));
properties.load(inputStream);
properties.setProperty(key, value);//保存到文件中(如果有的话会自动更新,没有会创建)
outputStream = new FileOutputStream(newFile(path));
properties.store(outputStream,"");
outputStream.close();
inputStream.close();
}catch(FileNotFoundException e) {
log.error("saveOrUpdateProperty error", e);
}catch(IOException e) {
log.error("saveOrUpdateProperty error", e);
}
}/*** 获取Properties
*
*@parampath
* 文件全路径
*@paramkey
*@return
*/
public staticString getPropertyValueByFilePath(String path, String key) {
Properties properties= newProperties();
InputStream inputStream;
String value= "";try{
log.info("path -> {}", path);
inputStream= new FileInputStream(newFile(path));
properties.load(inputStream);
value=properties.getProperty(key);//保存到文件中(如果有的话会自动更新,没有会创建)
inputStream.close();
}catch(FileNotFoundException e) {
log.error("saveOrUpdateProperty error", e);
}catch(IOException e) {
log.error("saveOrUpdateProperty error", e);
}returnvalue;
}/*** 获取Properties
*
*@parampath
* 文件全路径
*@return
*/
public staticProperties getPropertiesByFilePath(String path) {
Properties properties= newProperties();
InputStream inputStream;try{
log.info("path -> {}", path);
inputStream= new FileInputStream(newFile(path));
properties.load(inputStream);
inputStream.close();
}catch(FileNotFoundException e) {
log.error("saveOrUpdateProperty error", e);
}catch(IOException e) {
log.error("saveOrUpdateProperty error", e);
}returnproperties;
}/*** 获取Properties
*
*@parampath
* 文件全路径
*@paramkey
* key值
*@return
*/
public staticProperties removePropertyByFilePath(String path, String key) {
Properties properties= newProperties();
InputStream inputStream;
OutputStream outputStream;try{
log.info("path -> {}", path);
inputStream= new FileInputStream(newFile(path));
properties.load(inputStream);
log.info("properties -> {}", properties);if (properties != null &&properties.containsKey(key)) {
log.info("remove key:{}", key);
properties.remove(key);
}//保存到文件中(将properties保存到文件)
outputStream = new FileOutputStream(newFile(path));
properties.store(outputStream,"");
outputStream.close();
inputStream.close();
}catch(FileNotFoundException e) {
log.error("saveOrUpdateProperty error", e);
}catch(IOException e) {
log.error("saveOrUpdateProperty error", e);
}returnproperties;
}}