Java File List Load getFileList(String inputFilename)

Here you can find the source of getFileList(String inputFilename)

Description

getFileList If inputFilename is a normal file, and if each of its non-empty lines corresponds to a normal file, convert each to a File object and store in list.

License

Apache License

Parameter

Parameter Description
inputFilename Name of file containing filenames

Return

ArrayList

Declaration

public static ArrayList<File> getFileList(String inputFilename) throws IOException 

Method Source Code


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

import java.io.*;

import java.util.*;

public class Main {
    /**//w w w  .j  a  va 2  s  .c  o  m
     * getFileList
     *
     * If inputFilename is a normal file, and if each of its non-empty lines
     * corresponds to a normal file, convert each to a File object and store
     * in list. Otherwise, throw an appropriate exception.
     *
     * @param inputFilename Name of file containing filenames
     * @return ArrayList<File>
     */
    public static ArrayList<File> getFileList(String inputFilename) throws IOException {
        // Check that the input file exists, etc.
        File inputFile = getNormalFile(inputFilename);

        // Check that each filename in inputFile is a normal file
        // and store in fileList unless exception is encountered.
        ArrayList<File> fileList = new ArrayList<File>();

        FileReader fr = new FileReader(inputFile);
        BufferedReader br = new BufferedReader(fr);
        String filename;

        try {
            while ((filename = br.readLine()) != null) {
                // Ignore lines that are empty or only contain whitespace.
                filename = filename.trim();
                if (filename.length() == 0)
                    continue;

                // Add file object to fileList if it passes the tests.
                fileList.add(getNormalFile(filename));
            }
        } finally {
            fr.close();
        }
        return fileList;
    }

    /**
     * getNormalFile
     *
     * Create File object f from filename and check whether it exists,
     * is a normal, readable file and not a directory. If any of these
     * conditions are not met, throw an appropriate exception.
     *
     * @param filename Name of file to check
     * @return File object
     */
    public static File getNormalFile(String filename) throws IOException {
        File f = new File(filename);
        if (!f.exists())
            throw new FileNotFoundException(filename);
        if (!f.isFile())
            throw new IOException(filename + "is not a normal file.");
        if (!f.canRead())
            throw new IOException(filename + "is not readable.");
        return f;
    }
}

Related

  1. getFileList(String dir, final String suffix)
  2. getFileList(String directory, String filterRegexp)
  3. getFileList(String directoryPath)
  4. getFileList(String filePath, FileFilter fileFilter, boolean recursive)
  5. getFileList(String folderPath)
  6. getFileList(String path)
  7. getFileList(String path)
  8. getFileList(String path)
  9. getFileList(String path)