Java BufferedReader Read readFile(String pathname)

Here you can find the source of readFile(String pathname)

Description

Reads the file at the specified pathname and returns it as a List of Strings, one per line of the file.

License

Open Source License

Parameter

Parameter Description
pathname a String, the path of the file on the machine

Exception

Parameter Description
IOException if the file at pathname cannot be read

Return

a List of Strings representing the lines of the file at the specified pathname

Declaration

public static List<String> readFile(String pathname) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;
import java.util.ArrayList;

import java.util.List;

public class Main {
    /**/*from  w w  w .j a  v  a2 s.  c  o m*/
     * Reads the file at the specified pathname and returns it as a List of
     * Strings, one per line of the file. The newline terminators will be
     * stripped from each line when stored.
     *
     * @param pathname a String, the path of the file on the machine
     * @return a List of Strings representing the lines of the file
     * at the specified pathname
     * @throws IOException if the file at pathname cannot be read
     */
    public static List<String> readFile(String pathname) throws IOException {
        List<String> lines = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new FileReader(pathname));
        while (reader.ready()) {
            lines.add(reader.readLine());
        }
        reader.close();
        return lines;
    }
}

Related

  1. readFile(String path)
  2. readFile(String path)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFile(String sFileName_)
  7. readFile(String uri)
  8. readFile(String xmlFile)
  9. readFileAsHash(File file)