Here you can find the source of getAllFileNames(String path, String suffix, boolean isDepth)
Parameter | Description |
---|---|
path | String folder path |
suffix | String the specified suffix |
isDepth | boolean is need to scan all subdirectories |
public static List<String> getAllFileNames(String path, String suffix, boolean isDepth)
//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; } }