Java BufferedReader Read readFileAsListOfStrings(String filename)

Here you can find the source of readFileAsListOfStrings(String filename)

Description

Open and read a file, and return the lines in the file as a list of Strings.

Declaration

public static List<String> readFileAsListOfStrings(String filename) throws Exception 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**/*from   ww  w  . j  a  v a2s  .c om*/
     * Open and read a file, and return the lines in the file as a list of
     * Strings.
     */
    public static List<String> readFileAsListOfStrings(String filename) throws Exception {
        List<String> records = new ArrayList<String>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = reader.readLine()) != null) {
                records.add(line);
            }
            reader.close();
            return records;
        } catch (Exception e) {
            System.err.format("Exception occurred trying to read '%s'.", filename);
            e.printStackTrace();
            throw e;
        }
    }
}

Related

  1. readFileAsLines(File fileLocation)
  2. readFileAsLines(String aFile)
  3. readFileAsList(final String filename)
  4. readFileAsList(String filename, String splitter)
  5. readFileAsListOfString(String fileName, String splitBy)
  6. readFileAsListOfStrings(String filename)
  7. readFileAsText(String path)
  8. readFileByChar(String path)
  9. readFileContext(File fileToRead)