向后读取二进制文件
我需要做的是向后读取文件。我想要做的是向后读取文件,然后按原样向后移动查看值是否 = G 或十六进制 47,然后查看下一个值是否 = N 或十六进制 4E,然后查看下一个值是否 = P 。我目前正在使用二进制 IO 来读入,然后使用...
这是一个图像链接,可以更好地展示我的意思.. http://www.facebook.com/photo.php?pid=2189508&l=92393dfccb&id=1283154964
String s = Integer.toHexString(hexIn);
if(s.length() < 2){
s = "0" + Integer.toHexString(hexIn);
}
为了确保十六进制不会错过末尾的任何零(我在这个网站上找到的::)
What I need to do is read in a file backwards. What I would like to be able to do is read the file backwards then as is moves backwards see if the value = G or in hex 47 then see if the next value = N or in hex 4E and then see if the next value = P. I'm currently using the binary IO to read in and then using...
Here is a link to an image which will better show what I mean.. http://www.facebook.com/photo.php?pid=2189508&l=92393dfccb&id=1283154964
String s = Integer.toHexString(hexIn);
if(s.length() < 2){
s = "0" + Integer.toHexString(hexIn);
}
To make sure the hex does not miss any zeros on the end (which I found on this site::)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与其向后读取文件并查找字符串,为什么不直接反转字符串并在向前的文件中查找它呢?这似乎是一个更简单的解决方案?
Rather than read the file in backwards and look for your string, why don't you just reverse your string and look for it in the file going forwards? That would seem to be an easier solution?
正如你可以想象的那样,向后阅读并不是一件正常的事情,你必须做一些奇怪的事情。
但是您可以自己将索引放入文件中。如果你这样做,将其放在末尾,读取一个字节,然后将其放在 (end-1),读取一个字节,依此类推,你就会成功。
这会非常慢,所以你要做的就是从末尾开始将尽可能多的内容读入缓冲区,然后向后遍历缓冲区,然后重新填充缓冲区。
本教程告诉您有关 Java 中随机访问 I/O 的所有信息。
As you can imagine, reading backwards isn't the normal thing, and you'll have to do something weird.
But you can place the index into the file yourself. If you did that, placed it at the end, read a byte, then placed it at (end-1), read a byte, and so on, you'd succeed.
It would be horribly slow, so what you do iss read as much as you can into a buffer, starting at the end, then go through the buffer backward, then refill the buffer.
This tutorial tells you all about random access I/O in Java.
RandomAccessFile 将是用于向后读取文件的最简单的类。
RandomAccessFile would be the easiest class to use to read a file backwards.