Omar Omar - 7 months ago 16
Java Question

How to delete the contents of a file using the FileOutputStream class in Java?

I'm work with the FileOutputStream class in java, but I don't know how to delete the contents of a file (the main reason i need overwrite the file).... Thanks for your responses !

Answer

Call File.delete() which deletes the file or directory denoted by this abstract pathname.

File f = new File("foo.txt");
if (f.delete()) {
    System.out.println("file deleted");
}
Comments