1 Star 0 Fork 1.4K

javaalpha/DocSys

forked from Rainy/DocSys 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ReadProperties.java 1.85 KB
一键复制 编辑 原始数据 按行查看 历史
Rainy 提交于 2021-04-14 20:05 +08:00 . add file check
/**
* @Title: ReadProperties.java
* @Package util
* @Description: TODO
* @author zhanjp
* @date 2016年7月29日
*/
package util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;
/**
* ClassName: ReadProperties
* @Description: TODO
* @author zhanjp
* @date 2016年7月29日
*/
public class ReadProperties {
public static String read(String fileName, String key){
String basePath = new ReadProperties().getClass().getClassLoader().getResource("/").getPath();
String filePath = basePath+fileName;
return getValue(filePath, key);
}
public static String getValue(String filePath, String key){
try {
Properties props = new Properties();
File config = new File(filePath);
if(config.exists() == false)
{
return null;
}
InputStream in = new FileInputStream(config);
props.load(in);
return (String) props.get(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static boolean setValue(String filePath, String key, String value){
boolean ret = false;
FileOutputStream fileOutputStream = null;
try {
Properties props = new Properties();
File config = new File(filePath);
InputStream in = new FileInputStream(config);
props.load(in);
props.setProperty(key, value);
fileOutputStream = new FileOutputStream(filePath);
props.store(fileOutputStream, "");
ret = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fileOutputStream){
fileOutputStream.close();
}
} catch (Exception e) {
System.out.println("文件流关闭出现异常");
}
}
return ret;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/javaalpha/DocSys.git
git@gitee.com:javaalpha/DocSys.git
javaalpha
DocSys
DocSys
master

搜索帮助