In Java or Android development often encounter the need to delete a folder and its sub-files, this article mainly summarizes the development of the two encountered and used to delete files and folders method.
1. If you just delete a file, you can use the following code to pass in the file path
Public Static void DeleteFile (String path) { new File (path); File.delete (); }
2. If you need to delete a non-empty folder, you need to traverse the entire folder under the file, recursively delete
Public Static voidDeleteFolder (String path) {File file=NewFile (path); DeleteFile (file);} Public Static voiddeletefile (file file) {if(File.isfile ()) {file.delete (); return; } if(File.isdirectory ()) {file[] childfiles=File.listfiles (); if(Childfiles = =NULL|| Childfiles.length = = 0) {file.delete (); return; } for(inti = 0; i < childfiles.length; i++) {DeleteFile (childfiles[i]); } file.delete (); } }
deleting files in java/android development