问题
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."
回答1:
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
}
回答2:
Memory mapped IO, using a File Channel.... FileChannel.map(...)
来源:https://stackoverflow.com/questions/16840945/when-reading-a-binary-file-with-java-how