Java Text File Read by Charset readStreamToString(InputStream stream, Charset encoding)

Here you can find the source of readStreamToString(InputStream stream, Charset encoding)

Description

read Stream To String

License

Open Source License

Declaration

public static String readStreamToString(InputStream stream, Charset encoding) throws IOException 

Method Source Code

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

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

import java.nio.charset.Charset;

public class Main {
    private static final int BUFFER_SIZE = 4 * 1024;

    public static String readStreamToString(InputStream stream, Charset encoding) throws IOException {
        StringBuilder builder = new StringBuilder();
        InputStreamReader reader = new InputStreamReader(stream, encoding);
        char[] buffer = new char[BUFFER_SIZE];
        int length;
        while ((length = reader.read(buffer)) != -1) {
            builder.append(buffer, 0, length);
        }//from  w ww . j  a  v  a2  s  .com
        reader.close();
        return builder.toString();
    }
}

Related

  1. readLines(InputStream is, CharsetDecoder decoder)
  2. readLines(String filePath, Charset charset)
  3. readLines(String path, Charset charset)
  4. readNextWord(BufferedInputStream in, Charset cs)
  5. readStreamAsString(final InputStream iStream, Charset iCharset)
  6. readString(DataInput input, int length, Charset charset)
  7. readString(final InputStream in, final Charset charset)
  8. readString(final InputStream in, final Charset charset)
  9. readString(final InputStream input, final Charset charset)