1

I get a file personHashMap.ser with a HashMap in it. Here's the code how i create it:

String file_path = ("//releasearea/ToolReleaseArea/user/personHashMap.ser");
public void createFile(Map<String, String> newContent) {
    try{
        File file = new File(file_path);

        FileOutputStream fos=new FileOutputStream(file);
        ObjectOutputStream oos=new ObjectOutputStream(fos);

        oos.writeObject(newContent);
        oos.flush();
        oos.close();
        fos.close();

    }catch (Exception e){
        System.err.println("Error in FileWrite: " + e.getMessage());
    }

}

Now i want, when the program is running, that all five minutes update the file personHashMap.ser only with the content which changed. So the method i called:

public void updateFile(Map<String, String> newContent) {
    Map<String, String> oldLdapContent = readFile();
    if(!oldLdapContent.equals(ldapContent)){ // they arent the same, 
                                             // so i must update the file

    }   

}

But now i haven't any ideas how i can realise that.
And is it better for the performance to update only the new content or should i clean the full file and insert the new list again?

Hope you can Help me..

EDIT:

The HashMap includes i.e street=Example Street.
But now, the new street called New Example Street. Now i must update the HashMap in the File. So i can't just append the new content...

Michael Schmidt
  • 9,090
  • 13
  • 56
  • 80

3 Answers3

2

Firstly HashMap isn't really an appropriate choice. It's designed for in-memory usage, not serialization (though of course it can be serialized in the standard way). But if it's just 2kb, then go ahead and write the whole thing rather than the updated data.

Second, you seem to be overly worried about performance of this rather trivial method (for 2kb the write will take mere milliseconds). I would be worried more about consistency and concurrency issues. I suggest you look into using a lightweight database such as JavaDB or h2.

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • +1 for this great answer. But it's easy to work with HashMap. How should i save the Map to use it again? I'm not sure the file would be so small over the time... – Michael Schmidt Oct 08 '12 at 09:50
  • @dTDesign Just write out the whole thing again. Don't worry about performance until you need to. Test your program with large amounts of data and see what happens. – artbristol Oct 08 '12 at 09:53
  • ok thanks. i do it so. but for the learning effect, i want also try the other version, when i've time for that. i accept the answer. As soon as i found a solution for my method, i post it in my answer. thank you for your help! – Michael Schmidt Oct 08 '12 at 11:25
0

You can call the updateFile method in a loop and then call sleep for 5 minutes (5*60*1000 ms).

Thread.Sleep(300000); // sleep for 5 minutes

To append to your already existing file you can use :

FileOutputStream fooStream = new FileOutputStream(file, true);
Unni Kris
  • 3,081
  • 4
  • 35
  • 57
0

Use the constructor FileOutputStream(File file, boolean append), set the boolean append to true. It will append the text in the existing file.

Pr38y
  • 1,565
  • 13
  • 21
  • i think not that append is the right method. Take a look to my EDIT. – Michael Schmidt Oct 08 '12 at 09:34
  • You can use two option the one is to set `append false` so that it will overwrite the changes. The other option is - if the file already exist than read the content parse it as `String` or use `Regex` than replace the changes. – Pr38y Oct 08 '12 at 09:43