首頁  >  問答  >  主體

java RandomAccessFile 覆盖数据

迷茫迷茫2714 天前1129

全部回覆(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
  • 取消回覆