hello ,
can you tell me how to delete the previous contents in the file,while adding new contents in java????

hello ,
can you tell me how to delete the previous contents in the file,while adding new contents in java????

How about deleting the entire file and then creating a new file with the same name at the same location !!!

When you open an existing file and you try to write some stuff in it, the old content is automatically deleted and overwritten by the new data. There is an option at the constructor of BufferedWriter class that allows to append the new data or overwrite the old ones.

BufferedWriter writer = new BufferedWriter(new FileWriter("fileName"));

writer.write("some stuff");
writer.newLine();

writer.close();

Check the API for BufferedWriter for further information because I don't remember very well the exact syntax of the methods I used. You will also find what you are looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.