Java Text File Read Line readLines(String fileName)

Here you can find the source of readLines(String fileName)

Description

read Lines

License

Open Source License

Declaration

public static ArrayList<String> readLines(String fileName) 

Method Source Code

//package com.java2s;
/**//from  w  w w .  j  a  v  a2  s.  c  om
 * This software is released under the University of Illinois/Research and Academic Use License. See
 * the LICENSE file in the root folder for details. Copyright (c) 2016
 *
 * Developed by: The Cognitive Computation Group University of Illinois at Urbana-Champaign
 * http://cogcomp.cs.illinois.edu/
 */

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
    public static ArrayList<String> readLines(String fileName) {
        BufferedReader reader;
        try {
            reader = openReader(fileName);
        } catch (IOException e1) {
            System.out.println("Couldn't read file " + fileName);
            return null;
            // e1.printStackTrace();
        }
        String line;
        ArrayList<String> content = new ArrayList<String>();
        try {
            while ((line = reader.readLine()) != null) {
                content.add(line);
            }

            reader.close();

            return content;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static BufferedReader openReader(String fname) throws IOException {
        BufferedReader reader;

        InputStream is = ClassLoader.getSystemResourceAsStream(fname);

        if (is == null) {
            // try with a leading slash
            is = ClassLoader.getSystemResourceAsStream("/" + fname);

            if (is == null)
                is = new FileInputStream(fname);

        }

        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        return reader;

    }
}

Related

  1. readLines(String filename)
  2. readLines(String fileName)
  3. readLines(String filename)
  4. readLines(String fileName)
  5. readLines(String filename)
  6. readLines(String filename)
  7. readLines(String filename)
  8. readLines(String filename)
  9. readLines(String fileName)