- 我在屬性文件中插入了一些值。這將是一樣,
- 我要更新的屬性文件。在更新時,檢查密鑰是否可用。如果密鑰在那裏,我需要刪除密鑰和值,並且必須重新寫入。
- 任何人都可以給我的代碼刪除現有的密鑰和價值之前更新/再次寫入。
這裏是我的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, 「評論」); – 2013-03-06 06:10:19
可能重複的[從屬性文件中刪除鍵和值?](http://stackoverflow.com/questions/4225794/delete-key-and-value-from-a-property-file) – 2017-05-18 11:30:33