5

I am wondering what the best way to clear a file is. I know that java automatically creates a file with

f = new Formatter("jibberish.txt");  
s = new Scanner("jibberish.txt");

if none already exists. But what if one exists and I want to clear it every time I run the program? That is what I am wondering: to say it again how do I clear a file that already exists to just be blank? Here is what I was thinking:

public void clearFile(){
    //go through and do this every time in order to delete previous crap
    while(s.hasNext()){
        f.format(" ");
    }
} 
braX
  • 10,905
  • 5
  • 18
  • 32
Kemosabe
  • 301
  • 1
  • 4
  • 16
  • 1
    You should recheck your knowledge, because `Scanner(String)` won't create a file. – Tom Apr 26 '15 at 13:54

5 Answers5

11

Best I could think of is :

Files.newBufferedWriter(pathObject , StandardOpenOption.TRUNCATE_EXISTING);

and

Files.newInputStream(pathObject , StandardOpenOption.TRUNCATE_EXISTING);

In both the cases if the file specified in pathObject is writable, then that file will be truncated. No need to call write() function. Above code is sufficient to empty/truncate a file.This is new in java 8.

Hope it Helps

The Coder
  • 2,749
  • 4
  • 39
  • 68
  • Thanks for this.. It helped me resolve an issue with trailing string from previous file contents. – Adi B Oct 24 '19 at 08:02
7

You could delete the file and create it again instead of doing a lot of io.

if(file.delete()){
    file.createNewFile();
}else{
    //throw an exception indicating that the file could not be cleared
}

Alternately, you could just overwrite the contents of the file in one go as explained in the other answers :

PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();

Also, you are using the constructor from Scanner that takes a String argument. This constructor will not read from a file but use the String argument as the text to be scanned. You should first created a file handle and then pass it to the Scanner constructor :

File file = new File("jibberish.txt");
Scanner scanner = new Scanner(file);
Chetan Kinger
  • 14,713
  • 5
  • 41
  • 79
7

If you want to clear the file without deleting may be you can workaround this

public static void clearTheFile() {
        FileWriter fwOb = new FileWriter("FileName", false); 
        PrintWriter pwOb = new PrintWriter(fwOb, false);
        pwOb.flush();
        pwOb.close();
        fwOb.close();
    }

Edit: It throws exception so need to catch the exceptions

Ankur Anand
  • 3,777
  • 1
  • 22
  • 43
  • Are you sure the `flush` method clears the contents of the file? I thought `flush` would just make sure the stream is completely emptied.. – Chetan Kinger Apr 26 '15 at 13:38
  • the FileWriter in the second line throws an io exception, is there something to change rather than just throws IOException after clearFile() ? – Kemosabe Apr 26 '15 at 13:42
  • @Bagel here you will need to need to catch IOexception.. so just throws will work – Ankur Anand Apr 26 '15 at 13:44
  • @bot In printWriter i'm passing the Object of FileWriter .. Which will make sure its empty ! – Ankur Anand Apr 26 '15 at 13:50
2

You can just print an empty string into the file.

PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
1218985
  • 6,880
  • 2
  • 22
  • 27
1

type

new PrintWriter(PATH_FILE).close();
StreamsGit
  • 73
  • 7