Java Text File Read by Charset readAllLines(InputStream stream, Charset charset)

Here you can find the source of readAllLines(InputStream stream, Charset charset)

Description

Reads all lines using a BufferedReader and the given charset.

License

Open Source License

Parameter

Parameter Description
stream the stream to read from
charset the charset to use for reading from the stream

Exception

Parameter Description
IOException if an error during reading occurs

Return

the read lines

Declaration

public static List<String> readAllLines(InputStream stream, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    private static final Charset UTF8 = Charset.forName("UTF-8");

    /**/*from  w  w  w.java2 s.  c o m*/
     * Reads all lines via {@link #readAllLines(InputStream, Charset)} with UTF-8 as the charset.
     *
     * @param stream the stream to read from
     * @return the read lines
     * @throws IOException if an error during reading occurs
     */
    public static List<String> readAllLines(InputStream stream) throws IOException {
        return readAllLines(stream, UTF8);
    }

    /**
     * Reads all lines using a {@link BufferedReader} and the given charset.
     *
     * @param stream the stream to read from
     * @param charset the charset to use for reading from the stream
     * @return the read lines
     * @throws IOException if an error during reading occurs
     */
    public static List<String> readAllLines(InputStream stream, Charset charset) throws IOException {
        if (stream == null) {
            return Collections.emptyList();
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset))) {
            return reader.lines().collect(Collectors.toList());
        } catch (IOException e) {
            throw new IOException("An error occurred during reading lines.", e);
        }
    }
}

Related

  1. read(InputStream in, String charsetName)
  2. readAll(InputStream in, Charset charset)
  3. readAll(InputStream inputStream, Charset charset)
  4. readAll(InputStream inputStream, Charset encoding)
  5. readAllLines(File file, Charset cs, String newLineDelimiter)
  6. readAllLines(Path path, Charset cs)
  7. readAllText(File file, Charset charset)
  8. readAllText(File file, Charset charset)
  9. readAsString(InputStream in, Charset charset)