I want to copy the content of file \'A\' to file \'B\'. after the copying is done I want to clear the content of file \'A\' and want to write on it from its beginning. I can
You can use
FileWriter fw = new FileWriter(/*your file path*/);
PrintWriter pw = new PrintWriter(fw);
pw.write("");
pw.flush();
pw.close();
Remember not to use
FileWriter fw = new FileWriter(/*your file path*/,true);
True in the filewriter constructor will enable append.
All you have to do is open file in truncate mode. Any Java file out class will automatically do that for you.
Write an empty string to the file, flush, and close. Make sure that the file writer is not in append-mode. I think that should do the trick.
One of the best companion for java is Apache Projects and please do refer to it. For file related operation you can refer to the Commons IO project.
The Below one line code will help us to make the file empty.
FileUtils.write(new File("/your/file/path"), "")
How about below:
File temp = new File("<your file name>");
if (temp.exists()) {
RandomAccessFile raf = new RandomAccessFile(temp, "rw");
raf.setLength(0);
}