使用 Java 向后读取二进制文件
我通常使用以下方式读取二进制文件:
//What I use to read in the file normally
int hexIn;
for(int i = 0; (hexIn = in.read()) != -1; i++){
}
我需要做的是向后读取文件 我已经尝试过类似的操作...但它不起作用!我查看了很多帮助页面,但找不到任何内容,希望您能帮助我。
//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();
}
只是为了使事情变得复杂,我正在读取二进制文件并将其转换为十六进制使用
//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);
}
假设通常读取的十六进制是
10 11 12 13 14 15
如果它是向后读取的,那么它将被读取
51 41 31 21 11 01
我需要读取它
15 14 13 12 11 10
有人知道吗?因为我对它们一无所知,连我值得信赖的 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您根本不想“读取”该文件。您想要做的是使用 FileChannel 和覆盖在文件顶部的 MappedByteBuffer,然后简单地反向访问字节缓冲区。
这使得主机操作系统可以为您管理从磁盘中实际读取块的过程,而您只需在循环中向后扫描缓冲区即可。
请查看此页面了解一些详细信息。
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.
您可以使用 RandomAccessFile 类
:应该可以工作,但会比
InputStream
慢得多,因为在这里您无法从块读取中受益。You can use RandomAccessFile class:
This should work, but will be much slower than
InputStream
as here you can't benefit from block reading.您需要使用 RandomAccesssFile。然后您可以指定要读取的确切字节。
它的效率不是很高,但它允许您读取任何大小的文件。
取决于您使用哪种解决方案的具体要求。
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.
尝试以下方法怎么样..注意:这绝对不是那么有效,但我相信会起作用。
首先将整个输入流读入 ByteArray
http ://www.java-tips.org/java-se-tips/java.io/reading-a-file-into-a-byte-array.html
使用以下代码。
code-example.java
现在您可以弹出数组,并且每个字节都将以正确的顺序(从文件向后)排列。 (注意:您仍然需要将字节拆分为各自的十六进制字符)
http://mattfleming.com/node/11
How about trying the following.. NOTE: this is definitely not that efficient but I believe will work.
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
Use the following code.
code-example.java
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)
http://mattfleming.com/node/11
如果二进制文件较小,请考虑将其读入字节数组。然后您可以向后或以任何其他顺序执行必要的操作。这是使用 java 7 的代码:
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: