Java Text File Read by Charset readLines(InputStream input, Charset charset)

Here you can find the source of readLines(InputStream input, Charset charset)

Description

Returns the lines of an InputStream as a list of Strings.

License

Apache License

Parameter

Parameter Description
input the <code>InputStream</code> to read from.
charset a charset.

Return

the list of lines.

Declaration

public static List<String> readLines(InputStream input, Charset charset) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Haifeng Li/* w w  w.  ja v  a2  s . co  m*/
 *   
 * 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.
 *******************************************************************************/

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Returns the lines of an <code>InputStream</code> as a list of Strings.
     *
     * @param input  the <code>InputStream</code> to read from.
     * @return the list of lines.
     */
    public static List<String> readLines(InputStream input) throws IOException {
        final InputStreamReader reader = new InputStreamReader(input);
        return readLines(reader);
    }

    /**
     * Returns the lines of an <code>InputStream</code> as a list of Strings.
     *
     * @param input  the <code>InputStream</code> to read from.
     * @param charset a charset.
     * @return the list of lines.
     */
    public static List<String> readLines(InputStream input, Charset charset) throws IOException {
        final InputStreamReader reader = new InputStreamReader(input, charset);
        return readLines(reader);
    }

    /**
     * Returns the lines of a <code>Reader</code> as a list of Strings.
     *
     * @param input  the <code>Reader</code> to read from.
     * @return the list of lines.
     */
    public static List<String> readLines(Reader input) throws IOException {
        BufferedReader reader = input instanceof BufferedReader ? (BufferedReader) input
                : new BufferedReader(input);
        List<String> list = new ArrayList<>();
        String line = reader.readLine();
        while (line != null) {
            list.add(line);
            line = reader.readLine();
        }
        reader.close();
        return list;
    }
}

Related

  1. readFromInputStream(InputStream stream, Charset charset)
  2. readInputStream(InputStream stream, Charset cs)
  3. readInputStreamToString(InputStream instream, Charset charset)
  4. readLineFromStream(InputStream is, StringBuffer buffer, CharsetDecoder decoder)
  5. readLines(InputStream in, Charset cs)
  6. readLines(InputStream is, CharsetDecoder decoder)
  7. readLines(String filePath, Charset charset)
  8. readLines(String path, Charset charset)
  9. readNextWord(BufferedInputStream in, Charset cs)