Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-覆盖二进制文件的部分_Java_Io - Fatal编程技术网

Java-覆盖二进制文件的部分

Java-覆盖二进制文件的部分,java,io,Java,Io,我正在尝试覆盖文件的最后26个字节。基本上,我需要在那里放置一些整数和字节变量。我试图将DataOutputStream与FileOutputStream一起使用,但这些东西没有seek()方法或类似的方法。那么,如何从(文件大小-26)开始执行writeInt()?我看到有一个write方法接受偏移量,但我不确定它是否是我想要的,如果是,如何将int、long和byte变量转换为byte[]以传递给该方法 感谢您的建议使用RandomAccessFile您可以按照以下思路进行操作: File

我正在尝试覆盖文件的最后26个字节。基本上,我需要在那里放置一些整数和字节变量。我试图将DataOutputStream与FileOutputStream一起使用,但这些东西没有seek()方法或类似的方法。那么,如何从(文件大小-26)开始执行writeInt()?我看到有一个write方法接受偏移量,但我不确定它是否是我想要的,如果是,如何将int、long和byte变量转换为byte[]以传递给该方法


感谢您的建议

使用
RandomAccessFile
您可以按照以下思路进行操作:

File myFile = new File (filename);
//Create the accessor with read-write access.
RandomAccessFile accessor = new RandomAccessFile (myFile, "rws");
int lastNumBytes = 26;
long startingPosition = accessor.length() - lastNumBytes;

accessor.seek(startingPosition);
accessor.writeInt(x);
accessor.writeShort(y);
accessor.writeByte(z);
accessor.close();

我希望有帮助!让我知道它是否足够好。

他可能会直接使用
访问器.writeInt(int)
这类方法,而无需将所有内容打包到字节数组中。谢谢你提供了一个很好的示例,RandomAccessFile就是我要找的。