【发布时间】: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 个字节
标签: java swing netbeans byte datainputstream