2013-03-06 76 views
4
  • 我在屬性文件中插入了一些值。這將是一樣,

鍵=值使用java從屬性文件中刪除鍵和值

  • 我要更新的屬性文件。在更新時,檢查密鑰是否可用。如果密鑰在那裏,我需要刪除密鑰和值,並且必須重新寫入。
  • 任何人都可以給我的代碼刪除現有的密鑰和價值之前更新/再次寫入。

這裏是我的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"); 
    } 
    } 
} 
+0

我會用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

+0

可能重複的[從屬性文件中刪除鍵和值?](http://stackoverflow.com/questions/4225794/delete-key-and-value-from-a-property-file) – 2017-05-18 11:30:33

回答

6

加載:

Properties properties = new Properties(); 
properties.load(your_reader); 

然後使用remove()方法來刪除一個屬性:

properties.remove(your_key); 

終於寫出這個更改文件屬性文件:

properties.store(your_writer, null); 

UPDATE

我發佈這個更新您的評論後:

我想..我是什麼有刪除的價值越來越被,其不干擾舊內容..只是重寫文件再次..最初的文件有鍵1 = value1和鍵2 =值使用上面的代碼..,我試圖刪除鍵2,現在鍵1 =值

後的文件有,鍵1 =值鍵2 =值那麼今天的時間和日期此例嘗試,我試過了,它完美的工作,我認爲你的代碼有一些錯誤,如果它不起作用:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import java.util.Properties; 

public class Props { 

    public Props() { 
     try { 
      File myFile = new File("props.properties"); 
      Properties properties = new Properties(); 
      properties.load(new FileInputStream(myFile)); 
      properties.remove("deletethis"); 
      OutputStream out = new FileOutputStream(myFile); 
      properties.store(out, null); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[]args){ 
     new Props(); 
    } 
} 

props.properties 前:

#Wed Mar 06 11:15:24 CET 2013 
file=File 
edit=Edit 
deletethis=I'm gonna be deleted! 

道具。性能

#Wed Mar 06 11:15:24 CET 2013 
file=File 
edit=Edit 
+0

我試着..所得到的是,它不干擾較舊的內容..只是用刪除值再次重寫文件..最初文件有key1 = value1和key2 = value2 ..使用上面的代碼,我試圖刪除key2 ,現在的文件,key1 = value1 key2 = value2那麼今天的時間和日期之後,key1 = value1 – Rachel 2013-03-06 06:24:33

+0

@Rachel檢查我的更新,它適用於我 – BackSlash 2013-03-06 10:17:58

+0

雅我明白了..謝謝.. – Rachel 2013-03-06 10:21:37

0

最簡單和最明顯的解決方案是讀取輸入文件,通過它去行由行,檢查你需要的屬性取代。然後做任何你需要的改變並重寫整個文件。

您可能還想使用臨時文件來編寫新配置,然後用它替換現有配置。如果您的應用程序在進程中發生崩潰,這將有助於您。您仍然可以使用舊的配置。

0

嘗試

Properties props = new Properties(); 
    FileInputStream in = new FileInputStream(file); 
    props.load(in); 
    in.close(); 
    if (props.remove("key") != null) { 
     FileOutputStream out = new FileOutputStream(file); 
     props.store(out, ""); 
     out.close(); 
    } 
+0

我試過..所得到的是,它不干擾較舊的內容..只是用刪除值再次重寫文件..最初文件有key1 = value1和key2 = value2 ..使用上面的代碼,我試圖刪除key2 =現在的文件有,key1 = value1 key2 = value2那麼今天的時間和日期之後key1 = value1 – Rachel 2013-03-06 06:28:42

+0

它肯定有效,你需要調試它,例如替換props.store(out,「」);與props.store(System.out,「」);並看看發生了什麼 – 2013-03-06 06:37:34

+0

我檢查..它不工作 – Rachel 2013-03-06 06:53:44

2

你看着Apache Commons Configuration

我會嘗試這樣的:

import org.apache.commons.configuration.PropertiesConfiguration; 

PropertiesConfiguration config = new PropertiesConfiguration("myfile.properties");  
config.clearProperty("my.property"); 
config.save();