Java NIO Delete File Example Code Program

In this post, let us examine the various approaches that are available to delete a file in a specific folder using Java. We will see how to delete a file in Java using java.nio.file.Files class and the standard java.io.File class. At the end you will find that the NIO class offers very elegant solution to delete a file in Java. In order to get started, let us write a simple program to delete a file;

1 import java.nio.file.*;
2 class deleteFile {
3    public static void main (String[] args) throws Exception
4    {
5         Path path = FileSystems.getDefault().getPath("C:/myjava/test","test.txt");
6         boolean b=Files.deleteIfExists(path);
7    }
8  }
This program uses the new NIO classes to achieve the deletion of the file. This program can also throw the exceptions depending on quite a number of cases. It is worth catching these exceptions to prevent abnormal termination of the program. Some of the possible exceptions and the resolutions are documented below: If you try to delete a folder which has contents using this Java program, you will get the following error:

1 Exception in thread "main" java.nio.file.DirectoryNotEmptyException: C:\myjava\test
2 at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
3 at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source)
4 at java.nio.file.Files.deleteIfExists(Unknown Source)
5 at deleteFile.main(deleteFile.java:6) 


 In order to fix the issue, you need to make sure that the directory you are trying to delete is not empty. When I tried setting the file attribute to “read only” you will get the exception as per below: 

1 Exception in thread "main" java.nio.file.AccessDeniedException: C:\myjava\test\test.txt at
2 sun.nio.fs.WindowsException.translateToIOException(Unknown Source) at
3 sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) at
4 sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) at
5 sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source) at
6 sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source) at
7 java.nio.file.Files.deleteIfExists(Unknown Source) at
8 deleteFile.main(deleteFile.java:6)


Though this is not a documented exception [DirectoryNotEmptyException, IOException and SecurityException]. My understanding is that the exception is a form of IOException. It is also possible to delete a file in Java using the standard Java File (java.io.File) class in Java. A code snippet that you can use to delete a document is provided below:

 2 import java.io.File;
 3 class deleteFile {
 4    public static void main (String[] args) throws Exception
 5    {
 6         String tempFile = "C:/myjava/test";     
 7         File fileTemp = new File(tempFile);
 8         fileTemp.delete();      
 9    }
10  }

1 comment:

Anonymous said...

When I tried this example with a docx file (i.e. delete docx files using this Java program) and if the file is open, I get the following exception below

Exception in thread "main" java.nio.file.FileSystemException: C:\myjava\test\test.docx: The process cannot access the file because it is being used by another process.

at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source)
at java.nio.file.Files.deleteIfExists(Unknown Source)
at deleteFile.main(deleteFile.java:7)