private static Logger logger = Logger.getLogger(SysPropUtil.class);
private static Properties p = new Properties();
//相对路径
private static String path;//="\\autoUpdateConfig.properties";
/**
* 获取项目更目录(最后没有\)
* E:\Workspaces\MyEclipseWS\AutoUpdateTool
* @return
*/
public static String getConfigPath(){
String configPath=SysPropUtil.getValue("config.path");
if (StringUtil.isEmpty(configPath)) {
configPath=getProjectPath();
}
return configPath;
}
public static String getProjectPath(){
String projectPath=System.getProperty("user.dir");
if (StringUtil.isEmpty(projectPath)) {
projectPath= new File("").getAbsolutePath();
}
return projectPath;
}
public static String getClassesPath(){
return SysPropUtil.class.getClassLoader().getResource("").getPath();
}
public static byte[] getRSAPublicKey()throws Exception{
String publicKey=SysPropUtil.getValue("update.key");
if (publicKey==null) {
publicKey=UpdateService.getPublicKey();
}
return Base64.decodeBase64(publicKey);
}
public static void init(String configpath)throws Exception{
System.out.println("SysPropUtil init("+configpath+") ");
logger.debug("SysPropUtil init("+configpath+") ");
loadConfig(p,configpath);
}
private static void loadConfig(Properties prop,String configpath) throws Exception{
FileInputStream fis = null;
String configFile = new File("").getAbsolutePath()+File.separator+ configpath;
path=configFile;
logger.debug("loadConfig path : "+configFile);
System.out.println("loadConfig path : "+configFile);
try {
File file=new File(configFile);
if (!file.exists()) {
file.createNewFile();
}
fis = new FileInputStream(file);
prop.load(fis);
} catch(Exception e) {
e.printStackTrace();
System.out.println("load "+configpath+" error:" + e.getMessage());
throw e;
} finally{
if(fis!=null){
try {fis.close();} catch(Exception e) {}
}
}
}
public static String getValue(String key) {
if (p != null) {
try {
return new String (p.getProperty(key).getBytes(),"utf-8");
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
logger.error("SysPropUtil getValue key="+key+" exception : "+e);
return null;
}
} else {
return null;
}
}
public static void setValue(String key,String value) {
if (p != null) {
p.setProperty(key, value);
}
}
public static void saveValue(String key,String value) {
try {
OutputStream out = new FileOutputStream(path);
p.setProperty(key, value);
p.store(out, "update");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
Properties文件操作
最新推荐文章于 2022-03-21 10:17:07 发布
