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

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

Description

Reads the contents of a file line by line to a List of Strings.

License

Apache License

Parameter

Parameter Description
file the file to read
encoding the encoding to use, null means platform default

Exception

Parameter Description
IOException in case of an I/O error
UnsupportedEncodingException if the encoding is not supported bythe VM

Return

the list of Strings representing each line in the file

Declaration

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

Method Source Code

//package com.java2s;
/*//from   w w  w.  j a  v  a2s .c  o m
 * Copyright 2014 Hyberbin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Email:hyberbin@qq.com
 */

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

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import java.io.Reader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * <p>
     * Reads the contents of a file line by line to a List of Strings.
     * </p>
     * <p>
     * There is no readLines method without encoding parameter because the
     * default encoding can differ between platforms and therefore results in
     * inconsistent results.
     * </p>
     *
     * @param file the file to read
     * @param encoding the encoding to use, null means platform default
     * @return the list of Strings representing each line in the file
     * @throws IOException in case of an I/O error
     * @throws UnsupportedEncodingException if the encoding is not supported by
     * the VM
     * @since Commons IO 1.1
     */
    public static List readLines(File file, String encoding) throws IOException {
        InputStream in = new FileInputStream(file);
        try {
            return readLines(in, encoding);
        } finally {
            closeQuietly(in);
        }
    }

    public static List readLines(InputStream input, String encoding) throws IOException {
        if (encoding == null) {
            return readLines(input);
        } else {
            InputStreamReader reader = new InputStreamReader(input, encoding);
            return readLines(reader);
        }
    }

    public static List readLines(InputStream input) throws IOException {
        InputStreamReader reader = new InputStreamReader(input);
        return readLines(reader);
    }

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

    public static void closeQuietly(OutputStream output) {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

    /**
     * Unconditionally close an <code>InputStream</code>.
     * <p>
     * Equivalent to {@link InputStream#close()}, except any exceptions will be
     * ignored. This is typically used in finally blocks.
     *
     * @param input the InputStream to close, may be null or already closed
     */
    public static void closeQuietly(InputStream input) {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }
}

Related

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