Convert Contents Of A ByteArrayInputStream To String

后端 未结 5 1039
盖世英雄少女心
盖世英雄少女心 2020-12-30 23:11

I read this post but I am not following. I have seen this but have not seen a proper example of converting a ByteArrayInputStream to String using

5条回答
  •  盖世英雄少女心
    2020-12-30 23:36

    Use Scanner and pass to it's constructor the ByteArrayInputStream then read the data from your Scanner , check this example :

    ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(new byte[] { 65, 80 });
    Scanner scanner = new Scanner(arrayInputStream);
    scanner.useDelimiter("\\Z");//To read all scanner content in one String
    String data = "";
    if (scanner.hasNext())
        data = scanner.next();
    System.out.println(data);
    

提交回复
热议问题