Here you can find the source of getAllFiles(String path)
Parameter | Description |
---|---|
path | String folder path |
public static List<File> getAllFiles(String path)
//package com.java2s; //License from project: Apache License import java.io.*; import java.util.*; public class Main { /**//from w w w. j a v a 2 s . co m * get all files in a folder * * @param path String folder path * @return List<File> */ public static List<File> getAllFiles(String path) { List<File> fileList = new ArrayList<File>(); File file = new File(path); if (!file.exists()) { return fileList; } if (!file.isDirectory()) { return fileList; } 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()) { fileList.add(tempFile); } if (tempFile.isDirectory()) { List<File> allFiles = getAllFiles(tempFile.getAbsolutePath()); fileList.addAll(allFiles); } } return fileList; } /** * get all files with specified suffix in a folder * * @param path String folder path * @param suffix String the specified suffix * @return List<File> */ public static List<File> getAllFiles(String path, String suffix) { List<File> fileList = new ArrayList<File>(); File file = new File(path); if (!file.exists()) { return fileList; } if (!file.isDirectory()) { return fileList; } 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()) { if (suffix == null || "".equals(suffix)) fileList.add(tempFile); else { String filePath = tempFile.getAbsolutePath(); if (!suffix.equals("")) { int beginIndex = filePath.lastIndexOf("."); // the last '.' index before suffix String tempSuffix; if (beginIndex != -1) { tempSuffix = filePath.substring(beginIndex + 1, filePath.length()); if (tempSuffix.equals(suffix)) { fileList.add(tempFile); } } } } } if (tempFile.isDirectory()) { List<File> allFiles = getAllFiles(tempFile.getAbsolutePath(), suffix); fileList.addAll(allFiles); } } return fileList; } }