Java Text File Read by Charset newReader(InputStream input, Charset encoding)

Here you can find the source of newReader(InputStream input, Charset encoding)

Description

Creates a java.io.Reader from an input stream

License

Apache License

Parameter

Parameter Description
input the input stream
encoding the encoding to use when reading from the input stream

Return

a wrapping the given input stream

Declaration

public static Reader newReader(InputStream input, Charset encoding) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 uniVocity Software Pty Ltd
 *
 * 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.//  w  ww  . ja  v a2s . c  om
 ******************************************************************************/

import java.io.*;
import java.nio.charset.*;

public class Main {
    /**
     * Creates a {@link java.io.Reader} from an input stream
     * @param input the input stream
     * @return a {@link java.io.Reader} wrapping the given input stream
     */
    public static Reader newReader(InputStream input) {
        return newReader(input, (Charset) null);
    }

    /**
     * Creates a {@link java.io.Reader} from an input stream
     * @param input the input stream
     * @param encoding the encoding to use when reading from the input stream
     * @return a {@link java.io.Reader} wrapping the given input stream
     */
    public static Reader newReader(InputStream input, String encoding) {
        return newReader(input, Charset.forName(encoding));
    }

    /**
     * Creates a {@link java.io.Reader} from an input stream
     * @param input the input stream
     * @param encoding the encoding to use when reading from the input stream
     * @return a {@link java.io.Reader} wrapping the given input stream
     */
    public static Reader newReader(InputStream input, Charset encoding) {
        if (encoding != null) {
            return new InputStreamReader(input, encoding);
        } else {
            return new InputStreamReader(input);
        }
    }

    /**
     * Creates a {@link java.io.Reader} for a given a file
     * @param file the file to be read
     * @return a {@link java.io.Reader} for reading the given file
     */
    public static Reader newReader(File file) {
        return newReader(file, (Charset) null);
    }

    /**
     * Creates a {@link java.io.Reader} for a given a file
     * @param file the file to be read
     * @param encoding the encoding to be used when reading from the file
     * @return a {@link java.io.Reader} for reading the given file
     */
    public static Reader newReader(File file, String encoding) {
        return newReader(file, Charset.forName(encoding));
    }

    /**
     * Creates a {@link java.io.Reader} for a given a file
     * @param file the file to be read
     * @param encoding the encoding to be used when reading from the file
     * @return a {@link java.io.Reader} for reading the given file
     */
    public static Reader newReader(File file, Charset encoding) {
        FileInputStream input;
        try {
            input = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(e);
        }

        return newReader(input, encoding);
    }
}

Related

  1. initDecoder(Charset charset, ThreadLocal> localDecoder)
  2. inputStreamToString(InputStream input, Charset charset, boolean closeInputAfterRead)
  3. newBufferedFileReader(File file, Charset charset)
  4. newBufferedReader(URL url, Charset cs)
  5. newReader(File file, Charset charset)
  6. newReader(ReadableByteChannel ch, Charset cs)
  7. newUTF8CharSetReader(String filename)
  8. openNewCompressedInputReader(final InputStream inputStream, final Charset charset)
  9. read(File file, Charset charset)