JavaNIO在同一行上读取字符和整数二进制

JavaNIO在同一行上读取字符和整数二进制,java,binary,nio,Java,Binary,Nio,我试图掌握java.nio,但在读取以前编写的二进制文件时遇到了困难 单行“在748上” 我使用try-with-resources,所以我确信文件和频道都可以 在bytebuffer上,声明并分配了12个通道大小 在这里,问题从我的bytearray开始,我可以用for each来阅读它 我似乎找不到任何方法来处理这些数字 我用.get(xx,8,2)尝试了第二个缓冲区,但我不知道如何将2的byte[]数组转换为int try(FileInputStream file = new FileIn

我试图掌握java.nio,但在读取以前编写的二进制文件时遇到了困难
单行“在748上”

我使用try-with-resources,所以我确信文件和频道都可以

在bytebuffer上,声明并分配了12个通道大小

在这里,问题从我的bytearray开始,我可以用for each来阅读它
我似乎找不到任何方法来处理这些数字

我用.get(xx,8,2)尝试了第二个缓冲区,但我不知道如何将2的byte[]数组转换为int

try(FileInputStream file = new FileInputStream("data.dat");
        FileChannel channel = file.getChannel()){

        ByteBuffer buffer = ByteBuffer.allocate((12));

        channel.read(buffer);
        byte[] xx = buffer.array();

        System.out.println(xx.length);
        for (byte z:xx) {
            System.out.println((char)z);
        }

        for (int i = 0; i < xx.length; i++) {
                if (i<8)
                    System.out.print((char)xx[i]);
                if (i>=8)
                    System.out.println((int)xx[i]);
        }
try(FileInputStream file=newfileinputstream(“data.dat”);
FileChannel=file.getChannel()){
ByteBuffer buffer=ByteBuffer.allocate((12));
通道读取(缓冲区);
字节[]xx=buffer.array();
系统输出打印项次(xx.长度);
用于(字节z:xx){
System.out.println((char)z);
}
对于(int i=0;i
您的数字不是二进制的-它只是3个ASCII数字“7”、“4”和“8”。当您将ByteBuffer转换为数组时,使用ByteBuffer对您有什么好处?不使用InputStream或Reader/BufferedReader(或Scanner)的原因是什么你可能是对的。我已经用Bytebuffer写了这行。我在通道大小12上看到,我推断在0索引数组上,12位置是/0