Java Directory to File List getAllFileNamesByPath(String path)

Here you can find the source of getAllFileNamesByPath(String path)

Description

get all file names in a folder

License

Apache License

Parameter

Parameter Description
path String folder path

Return

String[]

Declaration

public static String[] getAllFileNamesByPath(String path) 

Method Source Code


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

import java.io.*;

import java.util.*;

public class Main {
    /**/*from   w ww.  j a  v a 2s  .c o  m*/
     * get all file names in a folder
     *
     * @param path String folder path
     * @return String[]
     */
    public static String[] getAllFileNamesByPath(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        if (!file.isDirectory()) {
            return null;
        }
        String[] tempList = file.list();
        List<String> fileList = new ArrayList<String>();
        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.getName());
            }
        }
        return fileList.toArray(new String[fileList.size()]);
    }
}

Related

  1. getAllFileName(String path)
  2. GetAllFileName(String path)
  3. getAllFileName(String path, ArrayList fileName)
  4. getAllFileNames(File basedir, String path)
  5. getAllFileNames(String path, String suffix, boolean isDepth)
  6. getAllFileNamesInDir(String folderName)
  7. getAllFileNamesWithExtension(String directory, final String extension)
  8. getAllFilePath(String filedir)
  9. getAllFiles(File dir)