Java File Name Get getFilenames(String dir, FilenameFilter filter)

Here you can find the source of getFilenames(String dir, FilenameFilter filter)

Description

get Filenames

License

Open Source License

Parameter

Parameter Description
dir directory to read
filter optional FilenameFilter to filter the files in $dir

Return

list of all the files in $dir that match the optional $filter, the list is sorted using Arrays.sort(...)

Declaration

public static String[] getFilenames(String dir, FilenameFilter filter) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;

public class Main {
    /**//w  ww.  ja v a  2  s  . c om
     * 
     * @param dir
     *            directory to read
     * @param filter
     *            optional FilenameFilter to filter the files in $dir
     * @return list of all the files in $dir that match the optional $filter,
     *         the list is sorted using Arrays.sort(...)
     */
    public static String[] getFilenames(String dir, FilenameFilter filter) {
        File[] files;
        if (filter == null) {
            files = (new File(dir)).listFiles();
        } else {
            files = (new File(dir)).listFiles(filter);
        }
        String[] filenames = new String[files.length];
        for (int i = 0; i < files.length; i++) {
            filenames[i] = files[i].getName();
        }
        Arrays.sort(filenames);
        return filenames;
    }
}

Related

  1. getFileNames(File zipFile, Pattern pattern)
  2. getFileNames(File[] files)
  3. getFilenames(File[] files)
  4. getFileNames(final String path, final String regex)
  5. getFileNames(final String pDataDir, final String pFilter)
  6. getFilenames(String dirctory)
  7. getFileNames(String directory, String file)
  8. getFileNames(String dirPath)
  9. getFilenames(String filenames)