我正在尝试覆盖文件的最后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();
我希望这能有所帮助!如果还有不够清晰的地方,请告诉我。
accessor.writeInt(int)
这种方法,而不是将所有内容打包成字节数组。 - Roman Zenka