Reputation: 3020
I am reading a stream using inputStream
. The code is below
buffer = new byte[8];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(buffer);
System.out.println(new String(buffer, 0, numBytes));
}
} catch (Exception ex) {
ex.printStackTrace();
}
But instead of getting characters i am getting somewhat like below
What are these and how can get actual characters.
Upvotes: 1
Views: 278
Reputation: 272347
You're reading bytes and naively translating into strings. That works in a simple scenario, but bytes make up characters in different combinations. This is called character encoding, and I suspect you're running into issues wrt. this.
At a minimum, you need to determine the original encoding of the file you're reading, and provide this encoding as a constructor argument to the String
. This will tell String how to translate an array of bytes to an array of characters (and hence a string)
See this document for more info on encodings.
Upvotes: 3
Reputation: 234795
You need to supply the appropriate character set argument to the String
constructor. (That's the 4th argument - missing in your code - and has the wrong default for your data.)
Otherwise the String
will misinterpret your data.
You may well find that
java.nio.charset.Charset.forName("UTF-8")
is the one you need.
Upvotes: 4