使用 Java 向后读取二进制文件

发布于 2024-10-15 02:50:19 字数 855 浏览 5 评论 0原文

我通常使用以下方式读取二进制文件:

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安静 2024-10-22 02:50:19

您根本不想“读取”该文件。您想要做的是使用 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.

不羁少年 2024-10-22 02:50:19

您可以使用 RandomAccessFile

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();

:应该可以工作,但会比 InputStream 慢得多,因为在这里您无法从块读取中受益。

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.

淡紫姑娘! 2024-10-22 02:50:19

您需要使用 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.

一花一树开 2024-10-22 02:50:19

尝试以下方法怎么样..注意:这绝对不是那么有效,但我相信会起作用。

  1. 首先将整个输入流读入 ByteArray
    http ://www.java-tips.org/java-se-tips/java.io/reading-a-file-into-a-byte-array.html

  2. 使用以下代码。

code-example.java

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

现在您可以弹出数组,并且每个字节都将以正确的顺序(从文件向后)排列。 (注意:您仍然需要将字节拆分为各自的十六进制字符)

  1. 如果您不想这样做,您也可以查看此站点。此代码将向后读取文件数据。
    http://mattfleming.com/node/11

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
安静 2024-10-22 02:50:19

如果二进制文件较小,请考虑将其读入字节数组。然后您可以向后或以任何其他顺序执行必要的操作。这是使用 java 7 的代码:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文