【发布时间】:2013-02-20 19:35:33
【问题描述】:
- 我的属性文件中有一些插入的值。就像,
键=值
- 我必须更新属性文件。更新时,正在检查密钥是否可用。如果 key 存在,我需要删除 key 和 value 并重新写入。
- 谁能给我代码来删除现有的键和值,然后再更新/写入。
这是我要插入和更新的 java 代码:
if (action.equals("insert")) {
if (con != null) {
if (key == null) {
//session.setAttribute(username, con);
out.println("****Connected Successfully****");
String rootPath=request.getSession().getServletContext().getRealPath("/");
System.out.println(rootPath);
String propPath=rootPath+"/WEB-INF/";
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(propPath+"importedDB.properties", true)));
out1.println(cname+"=jdbc:oracle:thin:@"+host+":"+port+"/"+service+","+username+","+password);
out1.close();
} else {
out.println("*Connection name "+cname+" already exists. Please try with another name");
}
}
}
if (action.equals("update")) {
if (con != null) {
if (key == null) {
out.println(cname+" is not available.");
} else {
String rootPath=request.getSession().getServletContext().getRealPath("/");
System.out.println(rootPath);
String propPath=rootPath+"/WEB-INF/";
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(propPath+"importedDB.properties", true)));
out1.println(cname+"=jdbc:oracle:thin:@"+host+":"+port+"/"+service+","+username+","+password);
out1.close();
out.println("updated successfully");
}
}
}
【问题讨论】:
-
我会使用 java.util.properties。为格式化道歉,我在 iPhone 上写这个并返回保存我的回复而不是换行。 :(。示例代码是:Properties p = new Properties(); p.load(inputStream); p.getProperty("key"); p.setProperty("key","value"); p.store(outputStream, "cmets");
标签: java properties properties-file