1. Because you need to delete files, you need the following permissions:
<Uses-Permission Android: Name = "android. Permission. write_external_storage"> </uses-Permission>
2. Core code
Package COM. example. deleteyoumi; import Java. io. file; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast;/** @ Author: zhenghaibo * Web: http://blog.csdn.net/nuptboyzhb * mail: zhb931706659@126.com * 2013- 4-28 Nanjing, njupt, China */public class deleteyoumimainactivity extends activity implementsonclicklistener {private button deletebutton; private final static string youmipathstring = "/mnt/sdcard/Android/data /. youmicache/"; // folder path @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_delete_you_mi_main); deletebutton = (butto N) findviewbyid (R. id. delete_youmi); deletebutton. setonclicklistener (this) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. delete_you_mi_main, menu); Return true ;}@ overridepublic void onclick (view v) {// todo auto-generated method stubswitch (v. GETID () {case R. id. delete_youmi: file = New file (youmipathstring); deletefile (File); mhandler. sendemptymessage (1); break; default: break;} handler mhandler = new handler () {public void handlemessage (Message MSG) {Switch (MSG. what) {Case 0: Toast. maketext (getapplicationcontext (), "the file or folder does not exist", toast. length_long ). show (); break; Case 1: Toast. maketext (getapplicationcontext (), "deleted successfully! ", Toast. length_long ). show (); break; default: Break ;}};}; /*** recursively delete files and folders ** @ Param file * the root directory to be deleted */Public void deletefile (File file) {If (file. exists () = false) {mhandler. sendemptymessage (0); return;} else {If (file. isfile () {file. delete (); return;} If (file. isdirectory () {file [] childfile = file. listfiles (); If (childfile = NULL | childfile. length = 0) {file. delete (); return;} For (file F: childfile) {deletefile (f);} file. delete ();}}}}
3. Expansion
This blog is based on deletion. Of course, this method can be used to traverse all files in a folder. For example, search for the hosts file.