Java Reader Read Line readLines(Reader input)

Here you can find the source of readLines(Reader input)

Description

read Lines

License

Apache License

Declaration

public static List readLines(Reader input) throws IOException 

Method Source Code

//package com.java2s;
/*//from www  . ja  v  a  2  s.c om
 * 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(Reader input)
  2. readLines(Reader input)
  3. readLines(Reader input)
  4. readLines(Reader input)
  5. readLines(Reader input)
  6. readLines(Reader input)
  7. readLines(Reader input)
  8. readLines(Reader input)
  9. readLines(Reader input)