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

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

Description

read String

License

Open Source License

Declaration

public static String readString(InputStream in, String charset) 

Method Source Code


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

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class Main {
    public static String readString(InputStream in) {
        return new String(readAll(in));
    }//  w ww.  j  ava2  s  .  co m

    public static String readString(InputStream in, String charset) {
        try {
            return new String(readAll(in), charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String readString(InputStream in, Charset charset) {
        return new String(readAll(in), charset);
    }

    public static byte[] readAll(InputStream in) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        copyTo(in, baos);
        return baos.toByteArray();
    }

    public static void copyTo(InputStream in, OutputStream out) {
        try {
            byte[] arr = new byte[1024];
            int read;

            while ((read = in.read(arr)) != -1) {
                out.write(arr, 0, read);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

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