Deleting Files and Directories - Java File Path IO

Java examples for File Path IO:File delete

Description

Deleting Files and Directories

Demo Code

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
  public static void main(String[] args) throws Exception {
    Path path = FileSystems.getDefault().getPath("C:/folder1/photos",
        "test_1.jpg");

    // delete the file
    try {//from www .ja v a2s  .  c  o m
      Files.delete(path);
    } catch (IOException | SecurityException e) {
      System.err.println(e);
    }
  }
}

Result

To delete a directory, it must be empty.

If the file is a symbolic link, then the symbolic link itself, not the final target, is deleted.


Related Tutorials