The first case for everyone to share Android traverse a specific directory of all files, including subdirectories, and delete the most recently created.
Private Boolean Deletelastfromfloder (String path) {Boolean success = false;
try {arraylist<file> images = new arraylist<file> ();
GetFiles (images, path);
File latestsavedimage = images.get (0); if (latestsavedimage.exists ()) {for (int i = 1; i < images.size (); i++) {File nextfile = Images.get
(i);
if (nextfile.lastmodified () > Latestsavedimage.lastmodified ()) {latestsavedimage = Nextfile;
} log.e ("Brady", "images =" + Latestsavedimage.getabsolutepath ());
Success = Latestsavedimage.delete ();
} catch (Exception e) {e.printstacktrace ();
return success; private void GetFiles (arraylist<file> filelist, String path) {file[] allfiles = new File (path). Listfiles ()
;
for (int i = 0; i < allfiles.length i++) {File file = Allfiles[i];
if (File.isfile ()) {filelist.add (file); else if (!fiLe.getabsolutepath (). Contains (". Thumnail")) {GetFiles (filelist, File.getabsolutepath ());
}
}
}
The second case describes the folder traversal Android code for your reference, as follows
Package com.once;
Import Java.io.File;
Import java.util.ArrayList;
Import java.util.LinkedList; /** * Folder Traversal * @author Once * * */public class Dirtraversal {//no recursion public static Linkedlist<file> ;
Listlinkedfiles (String strpath) {linkedlist<file> list = new linkedlist<file> ();
File dir = new file (strpath);
File File = Dir.listfiles ();
for (int i = 0; i < file.length i++) {if (File.isdirectory ()) list.add (file);
Else System.out.println (File.getabsolutepath ());
} File tmp;
while (!list.isempty ()) {tmp = (File) list.removefirst ();
if (Tmp.isdirectory ()) {file = Tmp.listfiles ();
if (file = = null) continue;
for (int i = 0; i < file.length i++) {if (File.isdirectory ()) list.add (file);
Else System.out.println (File.getabsolutepath ()); } else {System.out.println (tmp.getabsolutEpath ());
} return list; //recursion public static arraylist<file> listfiles (String strpath) {return refreshfilelist (strpath
); public static arraylist<file> refreshfilelist (String strpath) {arraylist<file> filelist = new Array
List<file> ();
File dir = new file (strpath);
File files = dir.listfiles ();
if (files = null) return null; for (int i = 0; i < files.length i++) {if (Files.isdirectory ()) {refreshfilelist (Files.getabsolutepath
());
else {if (Files.getname (). toLowerCase (). EndsWith ("Zip")) filelist.add (files);
} return filelist;
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.