简体   繁体   English

用Java同时读写文件

[英]Simultaneous read & write to file in Java

So my scenario is as follows: 所以我的方案如下:

I have a directory, and every X second a Java application polls the directory to see if any new files have been added. 我有一个目录,每隔X秒,一个Java应用程序轮询该目录以查看是否添加了任何新文件。 If a new file is detected (which may be transferred there by unknown FTP implementation), I want to have a look in the file to see if it ends with a specific string (ie "allData"). 如果检测到一个新文件(可能由未知的FTP实现转移到那里),我想查看该文件以查看它是否以特定字符串结尾(即“allData”)。 If the string exists, I will start to parse it for my needs since the file is considered to be successfully transferred. 如果字符串存在,我将开始解析它以满足我的需要,因为文件被认为是成功传输的。

So the problem is that the file being transferred may be really big (several GBs) and I don't want the parsing to start before the entire file has been completely transferred (which is indicated by the ending "allData" string). 所以问题是正在传输的文件可能非常大(几GB),我不希望在整个文件完全传输之前启动解析(由结尾的“allData”字符串表示)。 And when I want to have a look into the file for this string, the search has to be fast and low in memory consumption. 当我想查看该字符串的文件时,搜索必须快速且内存消耗低。 Due to this reason I use a RandomAccessFile object (read only mode) which sets to file descriptor to quite near the end of the file, and then I start to search using that FD and a BufferedReader. 由于这个原因,我使用RandomAccessFile对象(只读模式),它将文件描述符设置为非常接近文件的末尾,然后我开始使用该FD和BufferedReader进行搜索。

What my question is whether or not my file opening in the Java application will affect the file transfer being executed. 我的问题是我在Java应用程序中打开文件是否会影响正在执行的文件传输。 Because it might be the case that the application starts to read (to decide whether or not the entire file has been transferred) before the file has been transferred. 因为可能是在传输文件之前应用程序开始读取(以确定是否已传输整个文件)的情况。 Will anything nasty happen in such an implementation? 在这样的实现中会发生什么令人讨厌的事吗?

我只是让远程端使用ftp传输文件,然后使远程端将文件从“filename.file.incomplete”重命名为“filename.file”,然后你可以查询那些没有“不完整”的东西。结束。

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

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