2

I have a requirement to update a file (just one particular row) which contains value in form of key value.

app.num_hosts=4
app.resourceid=broker0

I was planning to read all the file in a map, then modify the particular field and rewrite the file. Is this a good way to do update a file? Which API could i use to write a map into a file?

By searching through the existing questions i couldn't find a way to do update just single row without rewriting the entire file.

zer0Id0l
  • 1,374
  • 4
  • 22
  • 36

2 Answers2

3

Sounds like you essentially want to use java.util.properties library.

public static void main(String[] args) {

    Properties prop = new Properties();
    OutputStream output = null;

    try {
        //load the file into properties object
        FileInputStream input = new FileInputStream("config.properties");    
        prop.load(input);
        input.close();

        // set the properties value
        output = new FileOutputStream("config.properties");
        prop.setProperty("app.num_hosts", "4");
        prop.setProperty("app.resourceid", "broker0");
        prop.store(output, null);


    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

This blog post outlines it further, but what your going to want to do is first read the properties file, make your updates and then write it back out.

NSjonas
  • 10,693
  • 9
  • 66
  • 92
  • will it work even if my properties file is not name as `config.properties` but `config.layout` instead? – zer0Id0l Mar 26 '14 at 03:14
  • I believe it should, but I'm not 100%. Theres one way to find out ;) – NSjonas Mar 26 '14 at 03:16
  • @rrs120486 - According to the answers to [this question](http://stackoverflow.com/questions/1318347/how-to-use-java-property-files), the file extension is irrelevant. – jahroy Mar 26 '14 at 03:22
  • 1
    I would assume it is since your just reading/writing it through a FileInputStream. Its only concerned with the binary data so why would it care what the extension is? Also, @rrs120486 I updated the answer to better fit your question – NSjonas Mar 26 '14 at 03:26
1

One option with Java Properties

Other wise, alternative options would be, you can use IO Api and manually update it like below:

1) Create a map, which has key and value that you want to going to update in file.

HashMap<String, String> replaceValesMap = new HashMap<String, String>();

2) Read a file from path as it gives you real path i.e. war/fileName.layout

String filepath = getServletContext().getRealPath("fileName.layout");

3) Create a method, which read file and replace value, return modified strings.

public static String getreportPdfString(HashMap<String, String> replaceValesMap,String fileppath){
    String generatedString = "";
     File file = new File(fileppath);
        StringBuffer strContent = new StringBuffer("");
        FileInputStream fin = null;
        try {
          fin = new FileInputStream(file);
          int ch;
        while ((ch = fin.read()) != -1)
          strContent.append((char) ch);
          fin.close();
        } catch (Exception e) {
          System.out.println(e);
        }
       String fileString= strContent.toString();
       for (Map.Entry<String, String> entry : replaceValesMap.entrySet()) {
           fileString = StringUtils.replace(fileString, entry.getKey(),entry.getValue());
        }

    return fileString;

}

4) Finally write into file:

try (PrintStream out = new PrintStream(new FileOutputStream("fileName.layout"))) {
    out.print(text);
}
bNd
  • 7,512
  • 7
  • 39
  • 72