用java读取二进制文件时,如何? [英] When reading a binary file with java, how?

查看:178
本文介绍了用java读取二进制文件时,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您想从您正在使用缓冲区读取的文件中查找某些十六进制值,并最终将一段代码解析为输出文件,那么执行此操作的最佳方法是什么?我不在寻找代码示例。

这是一个非常大的文件,需要多个解析。我应该将它全部读入数组吗?什么样的阵列?这将取决于什么是最好的搜索我正在寻找的十六进制值,我不知道你将如何在Java中搜索十六进制值。



你会怎么做?



编辑:全部事件序列。



对不起,我猜这会令人困惑。完全解释。我会搜索标记字节模式。找到它。然后我将处理接下来的103​​5字节,第12和13字节是我需要检查的值,以及另一个检查的第1034和1035字节。字节14是我需要解析文件的1016字节的开始。



事件序列应该是:获取第一个检查,检查。获取第二个检查,检查。如果这些检查失败,请在输出文件中写入一个固定字节值(作为标记)。

解决方案

需要多个解析吗?如果它们很好,请将1016个字节写入输出文件。与此同时?不知道这个要求是什么意思。



我可能会开始简单。将十六进制搜索字符串转换为我正在查找的字节数组。使用FileInputStream从文件中逐个读取几个字节,并保持总共有多少字节与当前文件字节处的搜索字节匹配。

  bStream = new BufferedInputStream(new FileInputStream(file)); 
byte [] bytes = new byte [4096];
int读取;
while((read = bStream.read(bytes))> 0){
//这些新字节是否与我寻找
// //计算边界等的模式匹配?
}


If you are wanting to find certain hex values from a file you are reading in with a buffer and eventually parse a piece of code to an output file, what is the best way of doing this? I'm not looking for example code.

It is a very large file with multiple parses required. Should I read all of it into an array? What kind of array? It would depend on what would be best for searching for the hex values I'm looking for and I'm not sure how you would search for a hex value in java.

How would you do it?

EDIT: Full sequence of events.

"Sorry yes I guess that would be confusing. Full explanation. What will happen is I search along for the marker byte pattern. Find it. Then I will deal with the next 1035 Bytes. The 12th and 13th byte is a value I need for a check as well as the 1034th and 1035th byte for another check. Byte 14 is the start of the 1016 bytes I need to parse to the file.

The sequence of events should be: Get first check, check. Get second check, check. If these checks fail write a certain constant byte value to the output file (as a flag). If they are good, write the 1016 bytes to the output file."

解决方案

Multiple parses required? at the same time? Not sure what that requirement means.

I would probably start simple. Convert the hex search string to the array of bytes I am looking for. Use a FileInputStream to read bytes from the file a few at a time, and keep a running total of how many of the bytes match the search bytes at the current file byte.

bStream = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[4096];
int read;
while ((read = bStream.read(bytes)) > 0) {
    // do these new bytes match the pattern I'm looking for
    // accounting for boundaries, etc
}

这篇关于用java读取二进制文件时,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆