How do I convert an InputStream to a String in Java?

后端 未结 7 2057
梦毁少年i
梦毁少年i 2020-12-08 15:41

Suppose I have an InputStream that contains text data, and I want to convert this to a String (for example, so I can write the contents of the stre

7条回答
  •  心在旅途
    2020-12-08 15:52

    Below code might help you.

    public String convertBytestoString(InputStream inputStream)
    {
        int bytes;
        byte[] buffer = new byte[1024];
    
        bytes = inputStream.read(buffer);
        String stringData = new String(buffer,0,bytes);
    
        return stringData;
    
    }
    

提交回复
热议问题