简体   繁体   English

如何通过偏移读取更改文本文件

[英]how to read changing text-file through offset

For example, there is one text file. 例如,有一个文本文件。

text files change. 文本文件更改。

test.txt = abcde

1min later 1分钟后

test.txt = cdefg
(a,b be deleted / f,g add)

in this situation, i read all the past file to e . 在这种情况下,我将所有过去的文件读到e

so i want to read the f of the current file. 所以我想读取当前文件的f (This is an example and no one knows what words will come.) (这是一个例子,没有人知道会有什么词。)

The way I search for the last word I read and make an offset is impossible because there is a possibility of duplication. 我搜索我读取的最后一个单词并进行偏移的方式是不可能的,因为有可能重复。

I would appreciate your help :) 我很感激你的帮助:)

The answer will depend on filesystem semantics, and use of buffers. 答案取决于文件系统语义和缓冲区的使用。 Java runs on top of an operating system, which provides an abstraction called "files", which java exposes through its own APIs (with FileInputStream and so on). Java运行在操作系统之上,操作系统提供称为“文件”的抽象,java通过其自己的API(使用FileInputStream等)公开。

If you are using a BufferedReader , that reader has a buffer, where it stores an in-memory copy of whatever it reads from the actual file (as reported by the underlying OS). 如果您使用的是BufferedReader ,那么该读取器具有一个缓冲区,它存储从实际文件中读取的内容副本(由底层操作系统报告)。 Therefore, if you change things on the file outside Java, there is no guarantee that your reader will return the latest, fresh version -- instead of whatever it read into the buffer a while ago. 因此,如果您在Java之外的文件上更改内容,则无法保证您的读者将返回最新的新版本 - 而不是之前读取的任何内容。

The safe way to get fresh contents is to: 获得新鲜内容的安全方法是:

  1. close the file 关闭文件
  2. open it again 再打开它
  3. seek to the position you want to read, and read the contents. 寻找你想要阅读的位置,并阅读内容。

Even then, you are not fully guaranteed to get really fresh contents, because it depends on the OS's way of handling writes (which in turn may depend on what device the files live on: network shares are not the same as spinning disks...). 即使这样,你也不能完全保证获得真正新鲜的内容,因为它取决于操作系统处理写入的方式(反过来可能取决于文件所处的设备:网络共享与旋转磁盘不同...... )。 There may be a delay between one application thinking that it has written things to disk (or worse: writing it to an internal buffer that has not yet been flushed to disk), and the actual file being completely written out and ready to be read by others. 一个应用程序认为它已将内容写入磁盘(或更糟糕的是:将其写入尚未刷新到磁盘的内部缓冲区)之间可能存在延迟,并且实际文件已完全写出并准备好由其他。 Note the emphasis on completely : you can get partially-updated versions of files if you read them while they are still being written elsewhere. 请注意完全强调:如果您在文件仍在其他地方编写时阅读它们, 可以获得部分更新的文件版本。

This is one of the reasons that people use databases: they guarantee that you never get partially-updated versions (by using transactions and atomic updates). 这是人们使用数据库的原因之一:它们保证您永远不会获得部分更新版本(通过使用事务和原子更新)。 There are better ways than files to communicate two applications on the same machine; 有比文件更好的方法在同一台机器上通信两个应用程序; for example, consider using sockets. 例如,考虑使用套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM