files

Hi, I Want to know if there is an easy way to clear the contents of a file or empty it without deleting it.Thanks
[141 byte] By [rahulman] at [2007-9-26 2:33:41]
# 1

Easy way do it is using the FileWriter...

make sure that the file is not readprotected...

You can create the filewriter like:

FileWriter fw = new FileWriter(fileName, true);

if you want to add (append) more text in the file...

import java.io.*;

public class ClearFile {

public ClearFile(String fileName) {

try{

FileWriter fw = new FileWriter(fileName);

fw.write("");

}catch( IOException io) { io.printStackTrace(); }

}

public static void main(String[] args) {

ClearFile clear = new ClearFile("D:\\hello.txt");

}

}

I hope it solve your problem!

760613 at 2007-6-29 9:57:10 > top of Java-index,Archived Forums,Java Programming...