Android如何遍历特定目录下所有文件

Android如何遍历特定目录下所有文件,第1张

概述第一个案例为大家分享了Android遍历特定目录下所有文件,包含子目录的,并删除最新创建的。

第一个案例为大家分享了AndroID遍历特定目录下所有文件,包含子目录的,并删除最新创建的。

 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());      }    }  }

第二个案例介绍了文件夹遍历AndroID代码,供大家参考,具体内容如下

package com.once; import java.io.file;import java.util.ArrayList;import java.util.linkedList;/** * 文件夹遍历 * @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 ArrayList<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;  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android如何遍历特定目录下所有文件全部内容,希望文章能够帮你解决Android如何遍历特定目录下所有文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1148812.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存