In this tutorial we will learn how to delete file in java . To delete file in java we can use delete() method of java.nio.file.Files class or delete() method of object of java.io.File . Let’s discuss different ways to delete file in java
- Deleting a File Using the Files Class
- Deleting a File Using the File Class
Delete a File Using the Files Class
In this example we will delete file in java using Files.delete() method.
Syntax :
public static void delete(Path path) throws IOException
This method take path of file as parameter , return nothing and throws exception on any fail.
package com.demo.file;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class DeleteFileUsingFiles {
public static void main(String[] args) {
String fileName = "C://java_examples//file1.txt";
try {
Files.delete(Paths.get(fileName));
System.out.println("file delete successfuly");
} catch (IOException e) {
e.printStackTrace();
}
}
}
If file does not exists then delete() method throws exception
java.nio.file.NoSuchFileException: C:\java_examples\file1.txt
So if delete file if exists in java then we should use Files.deleteIfExists() method .
Syntax :
public static boolean deleteIfExists(Path path) throws IOException
Its takes parameter path to the file to delete . retrun true if the file was deleted otherwise false
Its Throws DirectoryNotEmptyException , IOException and SecurityException .
package com.demo.file;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class DeleteFileUsingFiles2 {
public static void main(String[] args) {
String fileName = "C://java_examples//file1.txt";
try {
boolean result = Files.deleteIfExists(Paths.get(fileName));
if (result) {
System.out.println("File is deleted successfully!");
} else {
System.out.println("Sorry, unable to delete the file.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Deleting a File Using the File Class
We can delete file in java using delete() method of java.io.File class.
Syntax :
public boolean delete();
This method return true if and only if the file or directory is successfully deleted otherwise false. It throws SecurityException .
package com.demo.file;
import java.io.File;
public class DeleteFileUsingFile {
public static void main(String[] args) {
String fileName = "C://java_examples//file1.txt";
try {
File file = new File(fileName);
if (file.delete()) {
System.out.println(file.getName() + " is deleted!");
} else {
System.out.println("Sorry, unable to delete the file.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Delete file if exists then we should use deleteOnExit() method of File Class. deleteOnExit() method delete file when the virtual machine terminates . Files (or directories) are deleted in the reverse order that they are registered .
package com.demo.file;
import java.io.File;
public class DeleteFileUsingFile2 {
public static void main(String[] args) {
String fileName = "C://java_examples//file1.txt";
try {
File file = new File(fileName);
//deletes the file when JVM terminates
file.deleteOnExit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this article we have learned how to delete file or delete file if exists java in java using various ways . If you want learn more about file handling in java then i recommended try these examples .
References :