且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从文件中获取新内容

更新时间:2023-02-23 21:49:39

你实现它如下:


  1. 打开使用 RandomAccessFile

  2. 寻找上次文件结尾的位置。 (如果这是第一次,请寻找文件的开头。)

  3. 阅读,直至到达新的文件结尾。

  4. 记录文件结尾的位置。

  5. 关闭 RandomAccessFile

  1. Open the using RandomAccessFile
  2. Seek to where the end-of-file was last time. (If this is the first time, seek to the start of the file.)
  3. Read until you reach the new end-of-file.
  4. Record where the end-of-file is.
  5. Close the RandomAccessFile

将位置记录为距文件开头的字节偏移量,并使用相同的值进行搜索。

Record the position as a byte offset from the start of the file, and use the same value for seeking.

您可以修改上述内容以重复使用 RandomAccessFile 对象,而不是每次都打开/关闭它。

You can modify the above to reuse the RandomAccessFile object rather than opening / closing it each time.

UPDATE - RandomAccessFile 的javadoc是这里。寻找 seek getFilePointer 方法。

UPDATE - The javadocs for RandomAccessFile are here. Look for the seek and getFilePointer methods.