簡體   English   中英

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

[英]Delete key and value from properties file using 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");
    }
  }
}

加載它:

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

然后使用remove()方法刪除屬性:

properties.remove(your_key);

最后將此更改寫入文件屬性文件:

properties.store(your_writer, null);

UPDATE

我發表評論后發布此更新:

我試過..得到的是什么,它不會擾亂舊內容..只是用刪除值重寫文件..最初文件有key1 = value1和key2 = value2 ..使用上面的代碼,我試圖刪除key2 ,現在文件有,key1 = value1 key2 = value2然后今天的時間和日期后key1 = value1

試試這個例子,我試過,它運行得很完美,我認為你的代碼中有一些錯誤,如果它不起作用:

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!

props.properties 之后

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

你看過Apache Commons配置了嗎?

我會嘗試類似的東西:

import org.apache.commons.configuration.PropertiesConfiguration;

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

最簡單,最明顯的解決方案是讀取輸入文件,逐行檢查並檢查需要替換的屬性。 然后執行您需要的任何更改並重寫整個文件。

您可能還希望使用臨時文件來編寫新配置,然后用它替換現有配置。 如果您的應用有可能在流程中間崩潰,這將有助於您。 您仍然可以使用舊的配置。

嘗試

    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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM