0

We have a complex class containing Collection. Every object of this class should be written into file. Is there an efficient way to update the data of the files, instead of rewriting the whole content of the file. Example:

ArrayList<String> arr = new ArrayList<>();
        arr.add("foo");
        arr.add("bar");
        Gson gson = new Gson();
        System.out.println(gson.toJson(arr));
        arr.add("baz");
        System.out.println(gson.toJson(arr));

Output:

["foo","bar"]
["foo","bar","baz"]
3
  • Does this answer your question? Java File Replace Lines Commented Feb 7, 2022 at 11:40
  • @pringi as far as I'm concerned json objects are written without new line symbol, so I'm not sure if this works in my case. Commented Feb 7, 2022 at 12:29
  • Might be a duplicate of stackoverflow.com/q/70480667. What do you mean by "instead of rewriting the whole content of the file"? As pointed out in a comment on that other question the OS does not support inserting characters in the middle of a file. Commented Feb 7, 2022 at 20:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.