Java program to delete local files, java program implementation
Import java. io. File;
Public class Test {
Public static void main (String args []) {
Test t = new Test ();
DelFolder ("d:/test ");
System. out. println ("OK ");
}
// 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 ();
Java. io. File myFilePath = new java. io. 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 [I]);
} Else {
Temp = new File (path + File. separator + tempList [I]);
}
If (temp. isFile ()){
Temp. delete ();
}
If (temp. isDirectory ()){
DelAllFile (path + "/" + tempList [I]); // delete the files in the folder first
DelFolder (path + "/" + tempList [I]); // Delete the empty folder.
Flag = true;
}
}
Return flag;
}
}