简体   繁体   English

从字节数组中读取两个字节

[英]Reading two bytes from array of bytes

I am creating a simple aplication in Java, which allows me to read text file. 我在Java中创建一个简单的应用程序,它允许我读取文本文件。 I have a byte array which is wrapped into ByteBuffer: 我有一个包含在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循环来浏览文本文件:

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字符。 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. Unicode字符由2个字节(16位)组成,因此我认为bArray [i]保存第一个(更高)8位,随后的8位是该字符的第二部分。 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?? 所以,例如,如果我需要找出这个字符:“#”当前是否在索引i和i + 1上,我可以这样做吗? ("#" in binary representation: 0010 0011): (二进制表示中的“#”:0010 0011):

if (bArray[i] == (byte)10 && bArray[i+1] == (byte) 11)

Thanks for responds 谢谢你的回应

The simple answer is that you should not treat textual data as a stream of bytes. 简单的答案是,您不应将文本数据视为字节流。 Specifically that means: don't use ByteBuffer . 具体来说,这意味着: 不要使用ByteBuffer

Use an InputStreamReader , which knows how to interpret sequences of bytes using a given encoding. 使用InputStreamReader ,它知道如何使用给定的编码来解释字节序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM