Java BufferedReader Read readFileAsList(final String filename)

Here you can find the source of readFileAsList(final String filename)

Description

read File As List

License

Apache License

Parameter

Parameter Description
filename the name of the file to read

Return

the contents of the file, as a List of lines

Declaration

public static List<String> readFileAsList(final String filename) 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from   w  ww .ja  v  a  2s . c o  m
     * @param filename
     *            the name of the file to read
     * @return the contents of the file, as a {@code List<String>} of lines
     */
    public static List<String> readFileAsList(final String filename) {
        final File f = new File(filename);
        if (f.exists()) {
            return readFileAsList(f);
        }
        return null;
    }

    /**
     * @param file
     *            the {@code File} to read
     * @return the contents of the file, as a {@code List<String>} of lines
     */
    public static List<String> readFileAsList(final File file) {

        final List<String> contents = new ArrayList<String>();
        try {
            final BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                contents.add(new String(line));
            }
            br.close();
        } catch (final IOException e) {
            e.printStackTrace();
            return null;
        }

        return contents;
    }
}

Related

  1. readFile(String uri)
  2. readFile(String xmlFile)
  3. readFileAsHash(File file)
  4. readFileAsLines(File fileLocation)
  5. readFileAsLines(String aFile)
  6. readFileAsList(String filename, String splitter)
  7. readFileAsListOfString(String fileName, String splitBy)
  8. readFileAsListOfStrings(String filename)
  9. readFileAsListOfStrings(String filename)