Java Text File Read Line readLines(File file, String encoding)

Here you can find the source of readLines(File file, String encoding)

Description

Get the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding.

License

Open Source License

Parameter

Parameter Description
file a parameter
encoding a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static List<String> readLines(File file, String encoding) throws IOException 

Method Source Code

//package com.java2s;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.Reader;

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

public class Main {
    /**//from   www .j  a v a 2 s .c o  m
     * Get the contents of an <code>InputStream</code> as a list of Strings,
      * one entry per line, using the specified character encoding.
      * 
     * @param file
     * @param encoding
     * @return
     * @throws IOException
     */
    public static List<String> readLines(File file, String encoding) throws IOException {
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(openInputStream(file), encoding);
            return readLines(reader);
        } finally {
            reader.close();
        }
    }

    public static List<String> readLines(Reader input) throws IOException {
        BufferedReader reader = new BufferedReader(input);
        List<String> list = new ArrayList<String>();
        String line = reader.readLine();
        while (line != null) {
            list.add(line);
            line = reader.readLine();
        }
        return list;
    }

    /**
      * Opens a {@link FileInputStream} for the specified file, providing better
      * error messages than simply calling <code>new FileInputStream(file)</code>.
      * <p>
      * At the end of the method either the stream will be successfully opened,
      * or an exception will have been thrown.
      * <p>
      * An exception is thrown if the file does not exist.
      * An exception is thrown if the file object exists but is a directory.
      * An exception is thrown if the file exists but cannot be read.
      * 
      * @param file  the file to open for input, must not be <code>null</code>
      * @return a new {@link FileInputStream} for the specified file
      * @throws FileNotFoundException if the file does not exist
      * @throws IOException if the file object is a directory
      * @throws IOException if the file cannot be read
      */
    public static FileInputStream openInputStream(File file) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canRead() == false) {
                throw new IOException("File '" + file + "' cannot be read");
            }
        } else {
            throw new FileNotFoundException("File '" + file + "' does not exist");
        }
        return new FileInputStream(file);
    }
}

Related

  1. readLines(File file)
  2. readLines(File file, boolean ignoreComments)
  3. readLines(File file, int maxArraySize)
  4. readLines(File file, String charset)
  5. readLines(File file, String charsetName)
  6. readLines(File file, String encoding)
  7. readLines(File file, String outputLineEnding)
  8. readLines(File inputFile)
  9. readLines(File inputFile, String encoding)