I have a byte array bytes of UTF-8 encoded strings which I want to convert to a String.
bytes.length is about 130000
String str = new String(bytes, StandardCharsets.UTF_8);
should do the job.
However str gets the value '<Unreadable>'
Converting bytes line by line and printing it out works nicely.
However appending the lines in a StringBuilder fails as well. Again the content of the StringBuilder r will be '<Unreadable>'.
So I thought there might be an unreadable byte in the array.
But r.substring(60000, r.count) works well, and r.substring(1,60000), too.
Is there any problem with the size of the byte array?? Maximum size of String/StringBuilder is 2^32 - 1 so there should be no problem.
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputStreamReader reader = new InputStreamReader(bais);
BufferedReader in = new BufferedReader(reader);
// String readBuf = in.lines().collect(Collectors.joining()); gives '<Unreadable>'
String readed;
StringBuilder r = new StringBuilder();
while ((readed = in. readLine()) != null) {
System.out.println(readed); // works fine
r=r.append(readed);
}
After the loop r.toString() is '<Unreadable>' Any ideas why I cannot convert the byte array to a String/StringBuilder?
I had exactly the same. The String was "<Unreadable>" if it was made from file bigger then 50000 bytes. But String.lenght() showed 50000+ bytes! I found that the text "<Unreadable"> returned the IDE (Netbeans 11) in debug tools. So, the String was OK, but Netbeans didn't show the right content. It showed "<Unreadable>" instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With