Getting a String from an InputStream

suggest change

A String can be read from an InputStream using the byte array constructor.


public String readString(InputStream input) throws IOException {
    byte[] bytes = new byte[50]; // supply the length of the string in bytes here;
    return new String(bytes);

This uses the system default charset, although an alternate charset may be specified:

return new String(bytes, Charset.forName("UTF-8"));

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents