How To Delete A File In Java

There are a couple of approaches to remove a file in Java:

  • Utilizing the File.delete function
  • Employing the File.deleteOnExit function
  • Java File.delete method

To remove a file in Java, you can utilize the File.delete function from the File class. This method is responsible for eliminating the specified file or directory based on its abstract pathname. It's important to note that if the pathname points to a directory, the directory must be empty in order to be deleted. The method signature for this operation is as follows:

Example

public boolean delete()

The function will return a boolean value of true if the deletion of the file or directory is successful, otherwise it will return false.

Example

Example

import java.io.File;

public class FileDeleteExample

{

public static void main(String[] args)

{	

try

{  		

File f= new File("E:\\demo.txt");			//file to be delete

if(f.delete())						//returns Boolean value

{

System.out.println(f.getName() + " deleted");	//getting and printing the file name

}

else

{

System.out.println("failed");

}

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

Output:

When the file exist.

When the file does not exist.

Java File.deleteOnExit method

The method File.deleteOnExit is responsible for removing the specified file or directory identified by the abstract pathname. When utilizing deleteOnExit, the deletion of the file occurs in a reverse order. The file is scheduled for deletion when the JVM terminates. It is important to note that this method does not provide any return value. Once the deletion request is initiated, it cannot be undone, underscoring the need for caution when employing this method.

The method signature is:

Example

public void deleteOnExit()

This technique is commonly employed for removing temporary files. Temporary files are utilized for storing less critical and transient data that should be deleted upon JVM termination.

To manually remove the .temp file, we can utilize the File.delete function.

Example

In this instance, a temporary file named abc.temp is generated and will be removed once the program terminates.

Example

import java.io.File;

import java.io.IOException;

public class DeleteOnExitExample

{

public static void main(String[] args)

{

File temp;

try

{

temp = File.createTempFile("abc", ".temp");		//creating a .temp file

System.out.println("Temp file created at location: " + temp.getAbsolutePath());    

temp.deleteOnExit();					 //delete file on runtime exit

System.out.println("Temp file exists : " + temp.exists());

} 

catch (IOException e)

{

e.printStackTrace();

}

}

}

Output:

Let's see another example that deletes text file.

Example

Example

import java.io.File;  

import java.io.IOException;  

public class DeleteTextFileExample

{  

public static void main(String[] args)

{  

try

{

File file = new File("F:\\newfile.txt");    		 //creates a file instance

file.deleteOnExit(); 				 //deletes the file when JVM terminates

System.out.println("Done");

Thread.sleep(1000);  

}

catch(Exception e)

{

e.printStackTrace();

}

}  

}

Output:

Input Required

This code uses input(). Please provide values below: