我需要在java中自解压.exe文件中获取最后22个字节作为中心目录的末尾(没有命令行,请没有终端解决方案)。我尝试使用 bufferInputStream 读取 .exe 文件的内容并获得成功,但是当尝试使用
获取最后 22 个字节时BufferInputStream.read(byteArray, 8170, 22);
java 引发异常,表示它已关闭流。在这方面的任何帮助将不胜感激。谢谢。
最佳答案
我还没有尝试过这个,但我想你可以使用 MappedByteBuffer 来读取最后 22 个字节。
File file = new File("/path/to/my/file.bin");
long size = file.length();
FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
MappedByteBuffer buffer = channel.map(MapMode.READ_ONLY, size-22, 22);
然后只需将缓冲区刷新到数组中即可。
byte[] payload = new byte[22];
buffer.get(payload);
关于java - 如何在java中获取.exe文件的最后 'n'字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916100/