Here you can find the source of getFileList(String inputFilename)
Parameter | Description |
---|---|
inputFilename | Name of file containing filenames |
public static ArrayList<File> getFileList(String inputFilename) throws IOException
//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; } }