Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.text.TextUtils;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static List<String> listFilePath(File file) {
        List<String> list = new ArrayList<String>();
        for (File childFile : listFile(file)) {
            list.add(childFile.getPath());
        }
        return list;
    }

    public static List<String> listFilePath(String path) {
        List<String> list = new ArrayList<String>();
        for (File childFile : listFile(path)) {
            list.add(childFile.getPath());
        }
        return list;
    }

    public static List<File> listFile(File file) {
        List<File> list = new ArrayList<File>();
        List<File> dirList = new ArrayList<File>();
        if (file != null && file.exists()) {
            if (file.isFile()) {
                list.add(file);
            } else if (file.isDirectory()) {
                if (!isNoMediaDir(file)) {
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (File childFile : files) {
                            if (childFile.isFile()) {
                                list.add(childFile);
                            } else if (childFile.isDirectory()) {
                                dirList.add(childFile);
                            }
                        }
                        while (!dirList.isEmpty()) {
                            File dir = dirList.remove(0);
                            if (isNoMediaDir(dir)) {
                                continue;
                            }
                            File[] listFiles = dir.listFiles();
                            if (listFiles != null) {
                                for (File childFile : listFiles) {
                                    if (childFile.isFile()) {
                                        list.add(childFile);
                                    } else if (childFile.isDirectory()) {
                                        dirList.add(childFile);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    public static List<File> listFile(String path) {
        if (!TextUtils.isEmpty(path)) {
            return listFile(new File(path));
        }
        return null;
    }

    public static boolean isNoMediaDir(File dir) {
        boolean result = false;
        if (dir == null || dir.isFile()) {
            result = true;
        } else {
            File[] files = dir.listFiles();
            if (files != null) {
                for (File childFile : files) {
                    if (childFile.isFile() && ".nomedia".equals(childFile.getName())) {
                        result = true;
                        break;
                    }
                }
            }
        }
        return result;
    }
}