Home » Core Java » io » File » Delete File in Java Example

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Delete File in Java Example

In this example we are going to see how to delete a file in Java. Of course Java offers a very convenient API to perform deletion and creation. Most of them are placed in File class. We are going to use delete() methods that deletes the file or directory. If the file is deleted successfully, the methods returns true, else false. In order to delete a directory with this method, the directory has to be empty.
 
 
 
 
 
 

1. Using delete()

Here is how you can use File.delete() to delete a file.

JavaDeleteFileExample.java:

package com.javacodegeeks.core.file;

import java.io.File;

public class JavaDeleteFileExample {
	
	private static final String FILE_PATH = "F:\\nikos7\\Desktop\\testFile.txt";
	
	public static void main(String[] args){
		
    		if( JavaDeleteFileExample.deleteFile(FILE_PATH) ){
    			System.out.println("File is deleted!");
    		} else {
    			System.err.println("Failed to delete file.");
    		}
	}
	
	public static boolean deleteFile(String filePath){
		
		File file = new File(FILE_PATH);
		
		return file.delete();
	}
}

2. Using NIO Files.delete

Most of the times you need not look any further for other ways to delete a file. But there is one problem with the above method. In case of an error, it is not informative enough about the reason of the failure. As you can see, it does not through any exceptions in case of an error. NIO’s Files.delete(Path path) method offers that extremely useful feature.

Let’s see how you can use it :

JavaDeleteFileExample.java:

package com.javacodegeeks.core.file;

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

public class JavaDeleteFileExample {
	
	private static final String FILE_PATH = "F:\\nikos7\\Desktop\\testFile.txt";
	
	public static void main(String[] args){
		
		try {
			JavaDeleteFileExample.deleteFileNIO(FILE_PATH);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void deleteFileNIO(String filePath) throws IOException{
		Path path = Paths.get(filePath);
		Files.delete(path);
	}
}

Now when I run this program and "F:\\nikos7\\Desktop\\testFile.txt" does not exist in my system, here is the output I get :

java.nio.file.NoSuchFileException: F:\nikos7\Desktop\testFile.txt
	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.delete(Unknown Source)
	at java.nio.file.Files.delete(Unknown Source)
	at com.javacodegeeks.core.file.JavaDeleteFileExample.deleteFileNIO(JavaDeleteFileExample.java:36)
	at com.javacodegeeks.core.file.JavaDeleteFileExample.main(JavaDeleteFileExample.java:17)

So you get a very good idea of the reason your program failed. Of course, exception handling has many other advantages like error reporting, logging etc.

Download the Source Code

This was an example on how to delete a file in Java.

Download
You can download the full source code of this example here : JavaDeleteFileExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close