简体   繁体   English

Java中的并发读/写文件

[英]Concurrent read/write file in Java

I have to read a text file from my Java application. 我必须从我的Java应用程序中读取一个文本文件。

The file contains many rows and this file is updated every X minutes from an external unknown application that appends new lines to the file. 该文件包含许多行,此文件每隔X分钟从外部未知应用程序更新,该应用程序将新行附加到文件。

I have to read all the rows from the file and then I have to delete all the records that I've just read. 我必须从文件中读取所有行,然后我必须删除我刚读过的所有记录。

Is it possibile to let me read the file row by row, deleting each row I read and at the same time allowing the external application to append other rows to the file? 是否有可能让我逐行读取文件,删除我读取的每一行,同时允许外部应用程序将其他行追加到文件中?

This file is located in a Samba shared folder so I'm using jCIFS to read/write the file and BufferedReader Java class. 此文件位于Samba共享文件夹中,因此我使用jCIFS来读取/写入文件和BufferedReader Java类。

thanks in advance 提前致谢

I don't know the perfect solution to your problem, but I would solve it differently: 我不知道你的问题的完美解决方案,但我会以不同的方式解决它:

  • rename the file (give it a unique name with an timestamp) 重命名文件(给它一个带时间戳的唯一名称)
  • the appender job will then automatically re-create it 然后,appender作业将自动重新创建它
  • process your time-stamped files (no need to delete them, keep them in place so you can later check what happened) 处理带有时间戳的文件(无需删除它们,将它们保存到位,以便以后查看发生的情况)

Problem is we don't know how the external application write and/or reuse this file. 问题是我们不知道外部应用程序如何编写和/或重用此文件。 It could be a problem if you delete rows while the external application use a counter to run correctly... 如果在外部应用程序使用计数器正确运行时删除行,则可能会出现问题...

There is no good solution unless you know how the other app works. 除非您知道其他应用程序的工作原理,否则没有好的解决方案。

Is it possibile to let me read the file row by row, deleting each row I read and at the same time allowing the external application to append other rows to the file? 是否有可能让我逐行读取文件,删除我读取的每一行,同时允许外部应用程序将其他行追加到文件中?

Yes, you can open the same file for reading and writing from multiple processes. 是的,您可以打开同一个文件,以便从多个进程进行读写。 In Linux, for example, you will get two separate file descriptors for the same file. 在Linux中,例如,你会得到两个不同的文件描述符相同的文件。 For file writes under the size of PIPE_BUF, or 4096 bytes in Linux, it is safe to assume the operations are atomic, meaning the kernel is handling the locking and unlocking to prevent race conditions. 对于大小为PIPE_BUF的文件写入,或Linux中的4096字节,可以安全地假设操作是原子操作,这意味着内核正在处理锁定和解锁以防止竞争条件。

Assuming Process A is writing to the file has opened it as APPEND, then each time Process A tells the kernel to write() it will first seek to the size of the file (the end of the file). 假设进程A正在写入文件已将其打开为APPEND,那么每次进程A告诉内核write()它将首先寻找文件的大小(文件的结尾)。 That means you can safely delete data in the file from Process B as long it is done in between the write operations of Process A. And as long as the write operations from Process A don't exceed PIPE_BUF, Linux guarantees they will be atomic, ie Process A can spam write operations and process B can constantly delete/write data, and no funky behavior will result. 这意味着只要在进程A的写操作之间完成,就可以安全地从进程B中删除文件中的数据。只要进程A的写操作不超过PIPE_BUF,Linux就保证它们是原子的,即进程A可以垃圾邮件写入操作,进程B可以不断删除/写入数据,不会产生任何时髦的行为。

Java provides you with implemented File Locks . Java为您提供了已实现的文件锁 But it's important to understand that it is only "advisory," not "mandatory." 但重要的是要明白它只是“咨询”,而不是“强制性”。 Java does not enforce the restriction, both processes must implement a check to see if another process holds the lock. Java不强制执行限制, 两个进程都必须执行检查以查看另一个进程是否持有锁。

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

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