登陆

java RandomAccessFile 覆盖数据

# Java
迷茫迷茫1852 天前742 次浏览

全部回复(1)我要回复

  • 大家讲道理

    大家讲道理2017-04-17 14:24:26

    答案是不能。
    RandomAccessFile

    其中这一句话

    output operations write bytes starting at the file pointer and advance
    the file pointer past the bytes written

    RandomAccessFile 将文件视为一个byte数组,它修改其中某个位置的值的行为就是,将第i个位置的值写为新的值,然后将指针指向后一个元素。

    可以看出,这个类不会帮你完成所有剩下的值进行移位的功能。所以如果你需要完成“插入”的功能,需要自己把数组的后续内容读取出来,重新按顺序写入文件。

    stackoverflow参考:

    1. Inserting text in middle using RandomAccessFile removes some text after that

    回复
    0
  • 取消回复发送