ホームページ  >  に質問  >  本文

java RandomAccessFile 覆盖数据

迷茫迷茫2714日前1131

全員に返信(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
  • キャンセル返事