Java Text File Read by Charset readString(final InputStream in, final Charset charset)

Here you can find the source of readString(final InputStream in, final Charset charset)

Description

Reads a string from an input stream.

License

Open Source License

Parameter

Parameter Description
in the input stream to read
charset the character set of the string

Exception

Parameter Description
IOException if an I/O error occurs

Return

the string

Declaration

public static String readString(final InputStream in, final Charset charset) throws IOException 

Method Source Code


//package com.java2s;
import java.io.*;

import java.nio.charset.Charset;

public class Main {
    /**//from   w w w  .jav  a  2  s  . c  o  m
     * Reads a string from an input stream. This method uses the JVM's default character set. This method will not
     * close the InputStream. It is the responsibility of the calling code to cleanup after itself.
     *
     * @param in the input stream to read
     * @return the string
     * @throws IOException if an I/O error occurs
     */
    public static String readString(final InputStream in) throws IOException {
        return readString(in, Charset.defaultCharset());
    }

    /**
     * Reads a string from an input stream. This method will not close the InputStream. It is the responsibility of
     * the calling code to cleanup after itself.
     *
     * @param in the input stream to read
     * @param encoding the encoding of the string
     * @return the string
     * @throws IOException if an I/O error occurs
     */
    public static String readString(final InputStream in, final String encoding) throws IOException {
        return readString(in, Charset.forName(encoding));
    }

    /**
     * Reads a string from an input stream. This method will not close the InputStream. It is this responsibility of
     * the calling code to cleanup after itself.
     *
     * @param in the input stream to read
     * @param charset the character set of the string
     * @return the string
     * @throws IOException if an I/O error occurs
     */
    public static String readString(final InputStream in, final Charset charset) throws IOException {
        return readString(new InputStreamReader(new BufferedInputStream(in), charset));
    }

    /**
     * Reads a string from a reader. This method will not close the Reader. It is the responsibility of the calling
     * code to cleanup after itself.
     *
     * @param reader the reader to read
     * @return the string
     * @throws IOException if an I/O error occurs
     */
    public static String readString(final Reader reader) throws IOException {
        final StringBuilder sb = new StringBuilder();
        final char[] b = new char[2048]; // Could be anything from 2KB to 8KB in size depending on characters
        int n;
        while ((n = reader.read(b)) >= 0) {
            sb.append(b, 0, n);
        }
        return sb.toString();
    }
}

Related

  1. readNextWord(BufferedInputStream in, Charset cs)
  2. readStreamAsString(final InputStream iStream, Charset iCharset)
  3. readStreamToString(InputStream stream, Charset encoding)
  4. readString(DataInput input, int length, Charset charset)
  5. readString(final InputStream in, final Charset charset)
  6. readString(final InputStream input, final Charset charset)
  7. readString(InputStream in, int numBytes, Charset encoding)
  8. readString(InputStream in, String charset)
  9. readStringFromStream(InputStream stream, Charset charset)