开发者

Read binary file backwards using Java

I'm reading in binary files normally using:

//What I use to read in the file normally
int hexIn;
for(int i = 0; (hexIn = in.read()) != -1; i++){
}

What I need to do is read the file in backwards I have tried something along the lines of... but it does not work! I have looked a loads of help pages but can't find anything I hope you can help me please.

//How im trying to read in the file backwards
for(long i = 0, j = length - 1;  i < length; i++, j--){
int hexIn = 0;
hexIn = in.read();
}

Just to complacate things I'm reading the binary开发者_开发知识库 in and converting it to hex using

//This makes sure it does not miss and 0 on the end
String s = Integer.toHexString(hexIn);
if(s.length() < 2){
s = "0" + Integer.toHexString(hexIn);
}

Say the hex being read in normally is

10 11 12 13 14 15 

If it was being read in backwards it would be read in

51 41 31 21 11 01

I need to read it in

15 14 13 12 11 10

Does anyone have an idea? Because I'm all out of them, not even my trusty Java books know!


You don't want to "read" the file at all. What you want to do is use a FileChannel and a MappedByteBuffer overlaid on top of the file, then simply access the byte buffer in reverse.

This lets the host OS manage the actual reading of blocks from disk for you, while you simply scan the buffer backwards in a loop.

Look at this page for some details.


You can use RandomAccessFile class:

RandomAccessFile file = new RandomAccessFile(new File(fileName), "r");
long index, length;
length = file.length() - 1; 
for (index = length; index >= 0; index--) {
    file.seek(index);
    int s = file.read();
    //..
}
file.close();

This should work, but will be much slower than InputStream as here you can't benefit from block reading.


You would need to use a RandomAccesFile. Then you can specify the exact byte to read.

It won't be very efficient but it allows you to read a file of any size.

Depends on your exact requirement which solution you use.


How about trying the following.. NOTE: this is definitely not that efficient but I believe will work.

  1. First read the whole inputstream into a ByteArray http://www.java-tips.org/java-se-tips/java.io/reading-a-file-into-a-byte-array.html

  2. Use the following code.

code-example.java

byte[] theBytesFromTheFile = <bytes_read_in_from_file>
Array<Byte> bytes = new Array<Byte>();
for(Byte b : theBytesFromTheFile) {
    bytes.push(b);
}

Now you can pop the array and you will have each byte in the correct order, backwards from the file. (NOTE: You will still have to split the byte into their individual hex chars from the byte)

  1. If you don't want to do it this way, you can also look at this site. This code will read the files data in backward. http://mattfleming.com/node/11


In case of a small binary file consider reading it into byte array. Then you can perform necessary operations backwards or in any other order. Here is the code using java 7:

pivate byte[] readEntireBinaryFile(String filename) throws IOException {
  Path path = Paths.get(filename);
  return Files.readAllBytes(path);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜