// Delete the files in the buffer folder
String S = "D: // TXT // inbuffer //" + fileinfo [0] + "//" + fileid; // absolute path of the file
File file = new file (s );
If (file. exists ()){
Boolean d = file. Delete ();
If (d ){
System. Out. Print ("deleted successfully! ");
} Else {
System. Out. Print ("deletion failed! ");
}
}
Notes for deleting files in Java: 1. the paths cannot contain characters that are considered invalid by Java, such;
2. Ensure that the file is no longer used before the delete operation, that is, the file resource is released!
Java deletes files and folders:
1. Files in the folder;
2. subfolders (with files) in the folder );
3. subfolders in the folder (empty folder );
Routine:
Package test;
Import java. Io. file;
Public class deletefile {
// Delete a folder
// Complete absolute path of the param folderpath folder
Public static void delfolder (string folderpath ){
Try {
Delallfile (folderpath); // delete all contents
String filepath = folderpath;
Filepath = filepath. tostring ();
File myfilepath = new file (filepath );
Myfilepath. Delete (); // delete an empty folder
} Catch (exception e ){
E. printstacktrace ();
}
}
// Delete all objects in the specified folder
// Complete absolute path of the param path folder
Public static Boolean delallfile (string path ){
Boolean flag = false;
File file = new file (PATH );
If (! File. exists ()){
Return flag;
}
If (! File. isdirectory ()){
Return flag;
}
String [] templist = file. List ();
File temp = NULL;
For (INT I = 0; I <templist. length; I ++ ){
If (path. endswith (file. separator )){
Temp = new file (path + templist);
} Else {
Temp = new file (path + file. Separator + templist);
}
If (temp. isfile ()){
Temp. Delete ();
}
If (temp. isdirectory ()){
Delallfile (path + "/" + templist); // Delete the files in the folder first
Delfolder (path + "/" + templist); // Delete the empty folder.
Flag = true;
}
}
Return flag;
}
Public static void main (string [] ARGs ){
Deletefile T = new deletefile ();
Delfolder ("D:/1 ");
System. Out. println ("done ");
}
}