Delete File in Java

Deleting files in Java is really simple. You can just use delete method of File. Here is a sample code:

package file;
 
import java.io.File;
 
public class DeleteFile {
	public static void main(String[] args) {
		File file = new File("/home/programcreek/Desktop/s1");
 
		if (file.delete()) {
			System.out.println(file.getName() + " is deleted!");
		} else {
			System.out.println("File is not deleted.");
		}
	}
}

Leave a Comment