We must read other File operations like - create, copy, move, rename, exist?, append, hide/unHide, creation/lastModified/lastAccessed date, compare, size, Read only and writable.
java.io.File's delete() method can be used for deleting file.
Method returns true - if file is deleted successfully.
Method returns false - if file deleting failed
Program to Delete a file - in java file IO >
import java.io.File;
/** JavaMadeSoEasy.com */
public class DeleteFile {
public static void main(String... args) {
String deleteFileName = "c:/myFile.txt";
File deleteFile = new File(deleteFileName);
/*
* delete() method can be used for deleting file.
*
* Method returns true - if file is deleted successfully.
* Method returns false - if file deleting failed
*/
if(deleteFile.isFile()){ //Ensure it is file
boolean fileDeleteSuccess = deleteFile.delete();
if(fileDeleteSuccess){
System.out.println(deleteFileName +" deleted successfully");
}
else{
System.out.println(deleteFileName +" deletion failed");
}
}
else{
System.out.println(deleteFileName +" isn't a File");
}
}
}
/* OUTPUT
c:/myFile.txt deleted successfully
*/
|
RELATED LINKS>
Create File using createNewFile() method in java file IO
Program to Find file exists or not - in java file IO
Program on How to hide and unHide file or Directory in java 7 using java.nio.file
Program to Find file size in bytes, Kilobytes, megabytes and gigabytes using length method of File in java
Program to Find file is Read only or not in java 7
Labels:
Core Java
File IO/File handling