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

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

Description

read String From Stream

License

Open Source License

Declaration

static String readStringFromStream(InputStream stream, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
// Use of this source code is governed by a BSD-style license that can be

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

public class Main {
    static String readStringFromStream(InputStream stream, Charset charset) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();

        Reader reader = new InputStreamReader(stream, charset);

        char[] buffer = new char[1024];
        while (true) {
            int res = reader.read(buffer);
            if (res == -1) {
                break;
            }//from w  ww .ja va 2 s .com
            stringBuilder.append(buffer, 0, res);
        }
        reader.close();
        stream.close();

        return stringBuilder.toString();
    }
}

Related

  1. readString(final InputStream in, final Charset charset)
  2. readString(final InputStream in, final Charset charset)
  3. readString(final InputStream input, final Charset charset)
  4. readString(InputStream in, int numBytes, Charset encoding)
  5. readString(InputStream in, String charset)
  6. readTextFile(final String fileNamePath, final String charsetName)
  7. readTextStream(InputStream is, Charset charset)
  8. readToBuffer(StringBuffer buffer, String filePath, Charset charset)
  9. readToString(InputStream in, Charset charset)