List all the files from all the folder in a single list

試著忘記壹切 提交于 2019-11-26 07:25:27

问题


Hi I am looking for the solution to list all the files from root/Android device.

Suppose there are 3 folder inside root directory,but I want to display all the files in all of these folder in a single list..

Now If am using

       File f=new File(\"/sdcard\");

Then it will list all the files from the sdcard folder only..and If I will use

       File f=new File(\"/download\");

Then it will list all the files from download folder only ..and if I will use

       File f=new File(\"/\");

Then it will list only root direcoty files...not the files inside /sdcard or /download..

So what steps shall I follow to list all the files with a filter to list only .csv files from all the folder inside root.

Thanks..


回答1:


Try this:

 .....
 List<File> files = getListFiles(new File("YOUR ROOT")); 
 ....
 private List<File> getListFiles(File parentDir) {
    ArrayList<File> inFiles = new ArrayList<File>();
    File[] files = parentDir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            inFiles.addAll(getListFiles(file));
        } else {
            if(file.getName().endsWith(".csv")){
                inFiles.add(file);
            }
        }
    }
    return inFiles;
}

or variant without recursion:

private List<File> getListFiles2(File parentDir) {
    List<File> inFiles = new ArrayList<>();
    Queue<File> files = new LinkedList<>();
    files.addAll(Arrays.asList(parentDir.listFiles()));
    while (!files.isEmpty()) {
        File file = files.remove();
        if (file.isDirectory()) {
            files.addAll(Arrays.asList(file.listFiles()));
        } else if (file.getName().endsWith(".csv")) {
            inFiles.add(file);
        }
    }
    return inFiles;
}



回答2:


I've modified Vyacheslav's solution because I needed only file names inside the directory. Maybe it'll be helpfull for someone.

...
List<String> files = getList(new File("YOUR ROOT")); 
...
private List<String> getList(File parentDir, String pathToParentDir) {

    ArrayList<String> inFiles = new ArrayList<String>();
    String[] fileNames = parentDir.list();

    for (String fileName : fileNames) {
        if (fileName.toLowerCase().endsWith(".txt") || fileName.toLowerCase().endsWith(".rtf") || fileName.toLowerCase().endsWith(".txd")) {
            inFiles.add(pathToParentDir + fileName);
        } else {
            File file = new File(parentDir.getPath() + "/" + fileName);
            if (file.isDirectory()) {
                inFiles.addAll(getList(file, pathToParentDir + fileName + "/"));
            }
        }
    }

    return inFiles;
}



回答3:


You can use following method,

private int readLogList( String filePath )
{
    File directory = Environment.getExternalStorageDirectory();

    File folder = new File( directory + ConstantCodes.FILE_SEPARATOR + filePath );

    if ( !folder.exists() )
    {
        return 0;
    }

    return  folder.list().length;
}


来源:https://stackoverflow.com/questions/9530921/list-all-the-files-from-all-the-folder-in-a-single-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!