如何获得最后';n';java中.exe文件的字节数?
我需要在java的自解压.exe文件中将最后22个字节作为中心目录的结尾(请不要命令行,不要终端解决方案)。我尝试使用bufferInputStream读取.exe文件的内容,并获得了成功,但在尝试使用bufferInputStream获取最后22个字节时如何获得最后';n';java中.exe文件的字节数?,java,exe,self-extracting,Java,Exe,Self Extracting,我需要在java的自解压.exe文件中将最后22个字节作为中心目录的结尾(请不要命令行,不要终端解决方案)。我尝试使用bufferInputStream读取.exe文件的内容,并获得了成功,但在尝试使用bufferInputStream获取最后22个字节时 BufferInputStream.read(byteArray, 8170, 22); java提出了一个异常,说它是封闭流。在这方面的任何帮助都将不胜感激。谢谢。您首先需要从该文件创建一个FileInputStream File exe
BufferInputStream.read(byteArray, 8170, 22);
java提出了一个异常,说它是封闭流。在这方面的任何帮助都将不胜感激。谢谢。您首先需要从该文件创建一个FileInputStream
File exeFile = new File("path/to/your/exe");
long size = exeFile.length();
int readSize = 22;
try {
FileInputStream stream = new FileInputStream(exeFile);
stream.skip(size - readSize);
byte[] buffer = new byte[readSize];
if(stream.read(buffer) > 0) {
// process your data
}
else {
// Some errors
}
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我还没有尝试过这个,但是我想你可以使用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.io.IOException:流关闭的代码示例 您必须先检查inputstream,然后才能执行此操作
InputStream fis = new FileInputStream("c:/myfile.exe");
fis.close(); // only for demonstrating
// correct but useless
BufferedInputStream bis = new BufferedInputStream(fis);
byte x[]=new byte[100];
// EXCEPTION: HERE: if fis closed
bis.read(x,10,10);
请张贴代码和stacktrace