How do delete file in Java

In this example, we will demonstrate various ways to delete file in Java. Java offers rich I/O and NIO API to delete the file. Let us understand both in detail.

Delete File Using File.delete() method

You can use delete method available in java.io.File class to delete any file. It either returns true or false. If it returns true it means that file is successfully deleted whereas if it returns false which means that file was not deleted. 

Sample Code Snippet

import java.io.File;

public class DeleteFileExample {
	public static void main(String[] args) {

		try {
			File file = new File("C:\\Users\\amitgupta01\\Desktop\\Demo.txt");
			if (file.delete()) {
				System.out.println("File Deleted Successfully");
			} else {
				System.out.println("Unable to Delete the File");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

Output

File Deleted Successfully

Using NIO Files.delete() method

Often we need to handle the cases when the file is not successfully deleted in our code. As File.delete() method does not return the reason in the case when the file was not successfully deleted. NIO’s Files.delete() method solves the above problem by throwing exception when the file is not successfully deleted.

Let us demonstrate it using below code snippet

Sample Code Snippet

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

public class DeleteFileUsingNIO {
	public static void main(String[] args) {
		
		//File which exists on given path
		try{
			Path path = Paths.get("C:\\Users\\amitgupta01\\Desktop\\Demo.txt");
			Files.delete(path);
			System.out.println(" File " + path.getFileName() + " Successfully Deleted");
		}catch(IOException e){
			e.printStackTrace();
		}
		//File which does not exists on given path
		try{
			Path path1 = Paths.get("C:\\Users\\amitgupta01\\Desktop\\Demo1.txt");
			Files.delete(path1);
			System.out.println(" File " + path1.getFileName() + " Successfully Deleted");
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

Output

File Demo.txt Successfully Deleted
java.nio.file.NoSuchFileException: C:\Users\amitgupta01\Desktop\Demo1.txt
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
	at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
	at java.nio.file.Files.delete(Files.java:1079)
	at DeleteFileUsingNIO.main(DeleteFileUsingNIO.java:20)

In this article, you learned various approach to delete a file in Java.

Article tagged as
Author
Author: Amit Gupta
Published On: 12/01/2016
Last revised On: 13/01/2016
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media