Reputation: 1491
I am creating a simple aplication in Java, which allows me to read text file. I have a byte array which is wrapped into ByteBuffer:
FileInputStream inputStream = new FileInputStream(name);
FileChannel channel = inputStream.getChannel();
byte[] bArray = new byte[8192];
ByteBuffer byteBuffer = ByteBuffer.wrap(bArray);
int read;
and then I use a while loop to go through the text file:
while ( (read=channel.read(byteBuffer)) != -1 )
{
for ( int i=0; i<read; i++ )
//my code
byteBuffer.clear( );
}
My question is how to read a Unicode character in this case. Unicode characters consist of 2 bytes (16 bits) so I suppose that bArray[i] holds first (higher) 8 bits and the subsequent 8 bits is the second part of this character. So for instance if I need to find out whether this character: "#" is currently on index i and i + 1, can I do it like this?? ("#" in binary representation: 0010 0011):
if (bArray[i] == (byte)10 && bArray[i+1] == (byte) 11)
Thanks for responds
Upvotes: 3
Views: 1736
Reputation: 359836
The simple answer is that you should not treat textual data as a stream of bytes. Specifically that means: don't use ByteBuffer
.
Use an InputStreamReader
, which knows how to interpret sequences of bytes using a given encoding.
Upvotes: 6