Java Directory to File List getAllFileNames(String path, String suffix, boolean isDepth)

Here you can find the source of getAllFileNames(String path, String suffix, boolean isDepth)

Description

get all names of file with specified suffix in a folder

License

Apache License

Parameter

Parameter Description
path String folder path
suffix String the specified suffix
isDepth boolean is need to scan all subdirectories

Return

List

Declaration

public static List<String> getAllFileNames(String path, String suffix, boolean isDepth) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.util.*;

public class Main {
    /**//ww  w .j a va  2s  .c o m
     * get all names of file with specified suffix in a folder
     *
     * @param path    String folder path
     * @param suffix  String the specified suffix
     * @param isDepth boolean is need to scan all subdirectories
     * @return List<String>
     */
    public static List<String> getAllFileNames(String path, String suffix, boolean isDepth) {
        List<String> fileNamesList = new ArrayList<String>();
        File file = new File(path);
        return listFileName(fileNamesList, file, suffix, isDepth);
    }

    /**
     * get all file names in a folder
     *
     * @param path String folder path
     * @return List<String>
     */
    public static List<String> getAllFileNames(String path) {
        List<String> fileNamesList = new ArrayList<String>();
        File file = new File(path);
        if (!file.exists()) {
            return fileNamesList;
        }
        if (!file.isDirectory()) {
            return fileNamesList;
        }
        String[] tempList = file.list();
        File tempFile;
        for (String fileName : tempList) {
            if (path.endsWith(File.separator)) {
                tempFile = new File(path + fileName);
            } else {
                tempFile = new File(path + File.separator + fileName);
            }
            if (tempFile.isFile()) {
                fileNamesList.add(tempFile.getName());
            }
        }
        return fileNamesList;
    }

    private static List<String> listFileName(List<String> fileNamesList, File file, String suffix,
            boolean isDepth) {
        // if is directory, scan all subdirectories by recursion
        if (file.isDirectory()) {
            File[] fileList = file.listFiles();

            if (fileList != null) {
                for (File tempFile : fileList) {
                    if (isDepth || tempFile.isFile()) {
                        listFileName(fileNamesList, tempFile, suffix, isDepth);
                    }
                }
            }
        } else {
            String filePath = file.getAbsolutePath();
            if (!suffix.equals("")) {
                int begIndex = filePath.lastIndexOf("."); // the last '.' index before suffix
                String tempSuffix;

                if (begIndex != -1) {
                    tempSuffix = filePath.substring(begIndex + 1, filePath.length());
                    if (tempSuffix.equals(suffix)) {
                        fileNamesList.add(filePath);
                    }
                }
            } else {
                fileNamesList.add(filePath);
            }
        }
        return fileNamesList;
    }
}

Related

  1. getAllFileFromPath(File file, List fileList, String ed)
  2. getAllFileName(String path)
  3. GetAllFileName(String path)
  4. getAllFileName(String path, ArrayList fileName)
  5. getAllFileNames(File basedir, String path)
  6. getAllFileNamesByPath(String path)
  7. getAllFileNamesInDir(String folderName)
  8. getAllFileNamesWithExtension(String directory, final String extension)
  9. getAllFilePath(String filedir)