【问题标题】:How to only get the first 8 bytes from a file from a DataInputStream javajava - 如何仅从DataInputStream java的文件中获取前8个字节
【发布时间】:2016-11-21 13:44:17
【问题描述】:

目前正在编写一个java swing 程序。它从驱动器中获取所有文件并检查二进制签名。但我只希望文件的前 2-8 个字节加快编程速度。尝试了大多数已经可用的解决方案,但没有一个适用于我已经编码的内容。

当前代码:

 public void getBinary() {

    try {


        // get the file as a DataInputStream
        DataInputStream input = new DataInputStream(new FileInputStream(file));

        try {

            // if files are avaliable countinue looping and get bytes / hex 
            while (input.available() > 0) {

                // build  a hex string from the bytes and change to uppercase 
                sb.append(Integer.toHexString(input.readByte()).toUpperCase());


                // need to get the first couple of (8) bytes 
            }



        } catch (IOException e) {
        }

        // print the hex out to command 

        //  System.out.println(sb.toString());

    } catch (FileNotFoundException e2) {
    } 

}

【问题讨论】:

  • 究竟是什么问题,您尝试的解决方案“不兼容”是什么意思?您已经知道如何使用 readByte 读取一个字节,那么是什么阻止您读取超过第一个字节的内容?
  • 代码读取文件的字节是的,但我需要一个“字节限制”,它只读取文件的前 8 个字节而不是全部 2000 个字节,我不知道该怎么做。
  • 为什么不用八字节缓冲区的readFully()
  • 字节[] 缓冲区 = 新字节[8]; input.readFully(缓冲区);不起作用,它会删除前 8 个字节
  • 也许这对你有帮助。 this onethis one

标签: java swing netbeans byte datainputstream


【解决方案1】:

非常适合我,也可以帮助其他人!!!

https://i.stack.imgur.com/8SU5A.png

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2016-05-15
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多