Home » Java » Java: Removing content from Json file-Exceptionshub

Java: Removing content from Json file-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I wonder how to remove/empty an .json File.

I have found various Methods online, how to empty an JSON Objekt, however the Objekt that were given were defined and done in the very same file

What i want: To acces a .json File and delete all the content/empty the objekt.

How can i achieve this?
Filepath

EDIT
@Sagar Kharab
the Errors

How to&Answers:

You may simply truncate the file or overwrite it with “{}”.

Answer:

File file = new File(Paths.get("Path to your file").toString());
file.setWritable(true);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(Paths.get("Path to your file").toString()));
bufferedOutputStream.write("{}".getBytes());
bufferedOutputStream.flush();

I think this should work for you.