Deleting a file - Java File Path IO

Java examples for File Path IO:File delete

Introduction

To delete a file, create a File object for the file, and then call the delete method.

If the file is a directory, the directory must be empty to be deleted.

Demo Code

import java.io.File;
import java.io.IOException;

public class Main {
  public static void main(String[] arg) throws IOException {

    File f = new File("myfile.txt");
    if (f.delete())
          System.out.println("File deleted.");
    else/*from w  w w. ja  v a 2 s.  co m*/
          System.out.println("File not deleted.");


  }
}

Related Tutorials