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

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

Description

Convenient method to read a byte stream as a character stream.

License

Open Source License

Parameter

Parameter Description
input The stream of bytes to read.
charset Specify how to decode the bytes.

Return

A character stream of bytes properly decoded.

Declaration

public static Reader asReader(InputStream input, Charset charset) 

Method Source Code

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

import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;

import java.nio.charset.Charset;

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

    /**/*from w w  w. ja v a2 s.  c o  m*/
     * Convenient method to read a byte stream as a character stream.
     *
     * @param input
     *            The stream of bytes to read.
     *
     * @return A character stream of bytes decoded in UTF-8.
     */
    public static Reader asReader(InputStream input) {
        return asReader(input, UTF8);
    }

    /**
     * Convenient method to read a byte stream as a character stream.
     *
     * @param input
     *            The stream of bytes to read.
     * @param charset
     *            Specify how to decode the bytes.
     *
     * @return A character stream of bytes properly decoded.
     */
    public static Reader asReader(InputStream input, Charset charset) {
        return new InputStreamReader(input, charset);
    }
}

Related

  1. convert(File file, Charset from, String toEncoding, ByteArrayOutputStream bytearray, boolean headersOn, int totalLinesToRead)
  2. copy(Reader input, OutputStream output, Charset encoding)
  3. createBOMStrippedReader(InputStream stream, String defaultCharset)
  4. createBufferedReaderWithGuessedCharset(File file)