Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. 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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close