Java InputStream Read Line readLines(InputStream input, String encoding)

Here you can find the source of readLines(InputStream input, String encoding)

Description

read Lines

License

Apache License

Declaration

public static List readLines(InputStream input, String encoding) throws IOException 

Method Source Code

//package com.java2s;
/*// w  w  w  .j  a v  a 2  s . 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(InputStream input)
  2. readLines(InputStream input)
  3. readLines(InputStream input)
  4. readLines(InputStream input, String encoding)
  5. readLines(InputStream input, String encoding)
  6. readLines(InputStream input, String encoding)
  7. readLines(InputStream input, String encoding)
  8. readLines(InputStream inputStream)
  9. readLines(InputStream inputStream)