Java Text File Read by Charset inputStreamToString(InputStream input, Charset charset, boolean closeInputAfterRead)

Here you can find the source of inputStreamToString(InputStream input, Charset charset, boolean closeInputAfterRead)

Description

Reads the input stream till the end and return the reading as a string.

License

Open Source License

Parameter

Parameter Description
input the input stream
charset charset set
closeInputAfterRead true to close input stream after use false to leave it unclosed.

Exception

Parameter Description
IOException an exception

Return

the read string

Declaration

public static String inputStreamToString(InputStream input, Charset charset, boolean closeInputAfterRead)
        throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;

public class Main {
    /**//from ww  w.  j ava2 s  .  c om
     * Reads the input stream till the end and return the reading as a string.
     * @param input the input stream
     * @param charset charset set
     * @param closeInputAfterRead true to close input stream after use false to leave it unclosed.
     * @return the read string
     * @throws IOException
     */
    public static String inputStreamToString(InputStream input, Charset charset, boolean closeInputAfterRead)
            throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset));
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (sb.length() > 0) {
                sb.append("\n");
            }
            sb.append(line);
        }
        if (closeInputAfterRead) {
            input.close();
        }
        return sb.toString();
    }

    /**
     * Closes a Writer without throwing any exception if something went wrong.
     * @param device output writer, can be null.
     */
    public static void close(Writer device) {
        if (null != device) {
            try {
                device.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }

    /**
     * Close a Reader without throwing any exception if something went wrong.
     * @param device Reader, can be null.
     */
    public static void close(Reader device) {
        if (null != device) {
            try {
                device.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }

    /**
     * Close a stream without throwing any exception if something went wrong.
     * @param device stream, can be null.
     */
    public static void close(OutputStream device) {
        if (null != device) {
            try {
                device.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }

    /**
     * Close a stream without throwing any exception if something went wrong.
     * @param device stream, can be null.
     */
    public static void close(InputStream device) {
        if (null != device) {
            try {
                device.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }
}

Related

  1. getReader(InputStream is, String charset)
  2. getReader(String fileName, Charset cs)
  3. getReader(String path, Charset encoding)
  4. inferCharset(byte[] bytes, int bytesRead, Charset clientCharset)
  5. initDecoder(Charset charset, ThreadLocal> localDecoder)
  6. newBufferedFileReader(File file, Charset charset)
  7. newBufferedReader(URL url, Charset cs)
  8. newReader(File file, Charset charset)
  9. newReader(InputStream input, Charset encoding)