Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)
How do I clear the contents of a file that exists already so that it can be blank when I write some other data in it. p.s. it should just clear the data already present not delete the existing file and create a new one with the same name.

1 Answer

0 votes
by (13.1k points)

You can do it like this:

public static void clearFile()

    try{

    FileWriter fw = new FileWriter("FileName", false);

    PrintWriter pw = new PrintWriter(fw, false);

    pw.flush();

    pw.close();

    fw.close();

    }catch(Exception exception){

        System.out.println("Exception have been caught");

    }

}

Related questions

Browse Categories

...