我试图覆盖最后26个字节的文件.基本上我需要在那里放几个整数和字节变量.我正在尝试将DataOutputStream与FileOutputStream一起使用,但这些东西没有seek()方法或类似的东西.那我怎么能从(文件大小-26)开始写一个writeInt()?我看到有一个写方法接受偏移,但我不确定它是否是我想要的,如果是这样,如何将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();
Run Code Online (Sandbox Code Playgroud)
希望对您有所帮助!让我知道它是否足够好。
| 归档时间: |
|
| 查看次数: |
4116 次 |
| 最近记录: |