Java Text File Write by Charset writeStreamToString(InputStream is, String charsetName)

Here you can find the source of writeStreamToString(InputStream is, String charsetName)

Description

write Stream To String

License

LGPL

Declaration

public static String writeStreamToString(InputStream is, String charsetName) throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.BufferedReader;

import java.io.DataInputStream;

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

public class Main {
    public static String writeStreamToString(InputStream is, String charsetName) throws IOException {
        return writeStreamToString(is, Charset.forName(charsetName));
    }/*from  www . ja v a  2  s .  c o  m*/

    public static String writeStreamToString(InputStream is) throws IOException {
        return writeStreamToString(is, Charset.defaultCharset());
    }

    public static String writeStreamToString(InputStream is, Charset charset) throws IOException {
        BufferedReader br = null;
        StringBuffer result = new StringBuffer();
        try {
            InputStream in = new DataInputStream(is);
            br = new BufferedReader(new InputStreamReader(in, charset));
            String strLine;
            // Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                result.append(strLine + "\n");
            }
        } finally {
            br.close();
        }
        return result.toString();
    }
}

Related

  1. writeFile(String path, String content, Charset encoding)
  2. writeFile(String path, String output, Charset encoding)
  3. writer(OutputStream out, String charset)
  4. writer(String path, String content, String charset)
  5. writerBuffered(final File file, final Charset charset)
  6. writeString(OutputStream os, String string, CharsetEncoder encoder)
  7. writeString(OutputStream out, String charset, String value)
  8. writeStringToFile(File file, String s, String charset)
  9. writeStringToFile(String fileContent, String filePath, String charset)