user1079065
user1079065

Reputation: 2205

over write file in java

possible duplicate : Java: How to read a text file

this is my code:

this.fileMetaDataPrintWriter = 
new PrintWriter(new FileOutputStream(new File(fileName), false));

and in other method

fileMetaDataPrintWriter.write(somedata);

these both methods are called in a thread and the filehandle closed when thread exits.

Still my file is appended and not overwritten.

what is the mistake?

Upvotes: 0

Views: 55

Answers (1)

vsnyc
vsnyc

Reputation: 2257

I have verified that the following code snippet written directly in main method is overwriting the file contents. Perhaps it is some other interaction in code that is causing a different behavior for you? Test this snippet and verify if overwrite works correctly

PrintWriter fileMetaDataPrintWriter = new PrintWriter(new FileOutputStream(new File("appendtest.txt"), false), true);
fileMetaDataPrintWriter.write("This replaces contents of appendtest.txt");
fileMetaDataPrintWriter.close();

Upvotes: 1

Related Questions