Java BufferedReader Read readFile(String directory, String fileName)

Here you can find the source of readFile(String directory, String fileName)

Description

Reads and returns the contents of a file as an ArrayList.

License

Open Source License

Parameter

Parameter Description
directory the directory in which the file is located
fileName the name of the file

Exception

Parameter Description
IOException in the case of error during input or output

Return

ArrayList of strings which contain the given file's contents.

Declaration

public static ArrayList<String> readFile(String directory, String fileName) throws IOException 

Method Source Code

//package com.java2s;
/**/*ww w . ja  va 2  s .  c o  m*/
 * An xkcd comic-viewer made in Java.
 * Copyright (C) 2017 dcarpinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static final int OTHER = 0, WINDOWS = 1, MACINTOSH = 2, LINUX = 3;

    /**
     * Reads and returns the contents of a file as an ArrayList.
     *
     * @param directory the directory in which the file is located
     * @param fileName the name of the file
     * @return ArrayList of strings which contain the given file's contents.
     * @throws IOException in the case of error during input or output
     */
    public static ArrayList<String> readFile(String directory, String fileName) throws IOException {
        if (detectOS() == OTHER) {
            System.err.println("[FileUtil : readFile] Detected OS is not supported.");
            return null;
        }
        directory = checkDir(directory);
        if (!new File(directory + fileName).exists()) {
            System.err.println("[FileUtil : readFile] File does not exist.");
            return null;
        }
        try {
            BufferedReader br = new BufferedReader(new FileReader(directory + fileName));
            String line;
            List<String> listResult = new ArrayList<>();
            while ((line = br.readLine()) != null) {
                listResult.add(line);
            }
            br.close();
            return (ArrayList<String>) listResult;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Checks the operating system FilUtil is being used by. Only specifies
     * Windows, Macintosh, and Linux.
     *
     * @return integer in relation to detected operating system
     */
    public static int detectOS() {
        int osState;
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("windows")) {
            osState = WINDOWS;
        } else if (os.contains("mac")) {
            osState = MACINTOSH;
        } else if (os.contains("linux")) {
            osState = LINUX;
        } else {
            osState = OTHER;
        }
        return osState;
    }

    /**
     * Checks the given directory for its suffix, ('/' and '\', depending on the
     * detected OS. If no suffix is found, it is added to prevent errors in
     * other methods.
     *
     * @param directory the directory whose suffix is to be checked
     * @return the directory as a string, ensuring it ends with the correct character
     */
    public static String checkDir(String directory) {
        switch (detectOS()) {
        case OTHER:
            System.err.println("[FileUtil : checkDir] Detected OS is not supported.");
            return null;
        default:
            if (!directory.endsWith(File.separator)) {
                directory += File.separator;
            }
            return directory;
        }
    }
}

Related

  1. readFile(List fieldNames, String filename)
  2. readFile(Reader reader)
  3. readFile(String absoluteFilePath)
  4. readFile(String aFileName)
  5. readFile(String directory)
  6. readFile(String file)
  7. readFile(String file)
  8. readFile(String file)
  9. readFile(String file)