How Android traverses all files in a particular directory _android

Source: Internet
Author: User

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&gt ;
    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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.