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
0010 0011, shouldn't you only be checking ifbArray[i] == 0x0andbArray[i+1] == 0x23? Unicode is two bytes, and since "#" is part of the standard set of ASCII characters, it does not have any bits set in the higher byte, so its representation is0000 0000 0010 00110x0and0x23respectively