您的位置:首页 > 运维架构

创建properties文件保存在WEB项目的classes文件下

2013-10-24 15:08 197 查看
1、保存数据

private synchronized boolean saveFile(String url,String filename){
try {
String path = servletRequest.getSession().getServletContext().getRealPath("/");
System.out.println(path);
path+="WEB-INF\\classes\\";
File file = new File(path+filename);
if(file.exists()){
file.delete();
}
Properties prop = new Properties();
prop.setProperty("username",dbusername);
prop.setProperty("password",dbpassword);
prop.setProperty("url",url);
FileOutputStream fos = new FileOutputStream(file);
prop.store(fos, null);
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;

}


2、读取数据

private synchronized String readFile(){
String jsonString="{}";
try {
InputStream in = TestDbConnection.class.getClassLoader().getResourceAsStream(filenames[projectType]);
if(in!=null){
Properties properties = new Properties();
properties.load(in);
String nameString = properties.getProperty("username");
String password = properties.getProperty("password");
String vip = properties.getProperty("ip");
String vdbname = properties.getProperty("dbname");
String vtype = properties.getProperty("dbtype");
jsonString="{username:\""+nameString+"\",password:\""+password+"\",ip:\""+vip+"\",dbname:\""+vdbname+"\",dbtype:\""+vtype+"\"}";
System.out.println(" jsonString: "+jsonString);
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonString;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐