Java:如何处理尝试修改同一文件的两个进程

发布于 2024-11-15 02:47:12 字数 3252 浏览 2 评论 0原文

可能的重复:
如何使用 java 锁定文件(如果可能)

我有两个进程调用两个修改同一文本文件的Java 程序。我注意到文本文件的内容缺少数据。我怀疑当一个java程序获取文本文件的写入流时,我认为它会阻止另一个java程序修改它(就像当你打开一个文件时,你无法删除该文件)。除了数据库之外还有其他方法可以解决这个问题吗? (并不是说数据库解决方案不干净或不优雅,只是我们在操作这个文本文件时编写了很多代码)

编辑

事实证明,我针对这个问题犯了一个错误。我的文本文件中的数据丢失的原因是,

ProcessA: 不断向文本文件中添加数据行

ProcessB: 在开始时,加载所有行将文本字段放入 List 中。然后操作该列表的包含内容。最后,ProcessB 将列表写回,替换文本文件的内容。

这在顺序过程中效果很好。但是当一起运行时,如果ProcessA向文件添加数据,在ProcessB操作List期间,那么当ProcessB code> 将 List 写回,无论刚刚添加的 ProcessA 都将被覆盖。所以我最初的想法是在 ProcessB 写回 List 之前,同步文本文件和 List 之间的数据。因此,当我写回 List 时,它将包含所有内容。所以这是我的努力

public void synchronizeFile(){
    try {
        File file = new File("path/to/file/that/both/A/and/B/write/to");
        FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
        FileLock lock = channel.lock(); //Lock the file. Block until release the lock
        List<PackageLog> tempList = readAllLogs(file);
        if(tempList.size() > logList.size()){
            //data are in corrupted state. Synchronized them.
            for(PackageLog pl : tempList){
                if(!pl.equals(lookUp(pl.getPackageLabel().getPackageId(), pl.getPackageLabel().getTransactionId()))){
                    logList.add(pl);
                }
            }
        }
        lock.release(); //Release the file
        channel.close();
    } catch (IOException e) {
        logger.error("IOException: ", e); 
    }
}

所以 logListProcessB 想要写出的当前列表。因此,在写出之前,我读取文件并将数据存储到 tempList 中,如果 tempListlogList 不同,则同步它们。问题是,此时 ProcessAProcessB 当前都访问该文件,因此当我尝试锁定该文件并从中读取时 List; tempList = readAllLogs(file);,我要么得到 OverlappingFileLockException,要么得到 java.io.IOException:进程无法访问文件,因为另一个进程已锁定文件的一部分。请帮我解决这个问题:(

EDIT2:我对锁定的理解

public static void main(String[] args){
    File file = new File("C:\\dev\\harry\\data.txt");

    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    FileChannel channel = null;
    FileLock lock = null;
    try{
        channel  = new RandomAccessFile(file, "rw").getChannel();
        lock = channel.lock();
        fileReader = new FileReader(file);
        bufferedReader = new BufferedReader(fileReader);
        String data;
        while((data = bufferedReader.readLine()) != null){
            System.out.println(data);
        }
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        try {
            lock.release();
            channel.close();
            if(bufferedReader != null) bufferedReader.close();
            if(fileReader != null) fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

,我得到了这个错误IOException:该进程无法访问该文件,因为另一个进程已锁定该文件的一部分

Possible Duplicate:
How can I lock a file using java (if possible)

I have two process that invoke two Java programs that modifying the same text file. I notice the content of the text file is missing data. I suspect that when one java program obtain the write stream to the text file, I think it block the other java program from modifying it(like when you have a file open, you cant delete that file). Is there a way to work around this other than database? (not to say that db solution is not clean or elegant, just that we wrote lot of codes in manipulating this text file)

EDIT

It turns out that I made a mistake targeting the problem. The reason why, data in my text file is missing is because,

ProcessA: keep add rows of data to the text file

ProcessB: at the beginning, load all the rows of the text field into a List. It is then manipulate the contains of that list. At the end, ProcessB write the list back out, replacing the contain of the text file.

This work great in sequential process. But when running together, if ProcessA adding data to the file, during the time ProcessB manipulating the List, then when ProcessB write the List back out, whatever ProcessA just add, will be overrided. So my initial thought is before ProcessB write the List back out, sync the data between text file and the List. So when I write the List back out, it will contains everything. so here is my effort

public void synchronizeFile(){
    try {
        File file = new File("path/to/file/that/both/A/and/B/write/to");
        FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
        FileLock lock = channel.lock(); //Lock the file. Block until release the lock
        List<PackageLog> tempList = readAllLogs(file);
        if(tempList.size() > logList.size()){
            //data are in corrupted state. Synchronized them.
            for(PackageLog pl : tempList){
                if(!pl.equals(lookUp(pl.getPackageLabel().getPackageId(), pl.getPackageLabel().getTransactionId()))){
                    logList.add(pl);
                }
            }
        }
        lock.release(); //Release the file
        channel.close();
    } catch (IOException e) {
        logger.error("IOException: ", e); 
    }
}

So logList is the current List that ProcessB want to write out. So before the write out, I read the file and store data into tempList, if tempList and logList are not the same, sync them. The problem is that at this point, both ProcessA and ProcessB currently access the file, so when I try to lock the file, and read from it List<PackageLog> tempList = readAllLogs(file);, I either get OverlappingFileLockException, or java.io.IOException: The process cannot access the file because another process has locked a portion of the file. Please please help me fix this problem :(

EDIT2: My understand of Lock

public static void main(String[] args){
    File file = new File("C:\\dev\\harry\\data.txt");

    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    FileChannel channel = null;
    FileLock lock = null;
    try{
        channel  = new RandomAccessFile(file, "rw").getChannel();
        lock = channel.lock();
        fileReader = new FileReader(file);
        bufferedReader = new BufferedReader(fileReader);
        String data;
        while((data = bufferedReader.readLine()) != null){
            System.out.println(data);
        }
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        try {
            lock.release();
            channel.close();
            if(bufferedReader != null) bufferedReader.close();
            if(fileReader != null) fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

and I got this error IOException: The process cannot access the file because another process has locked a portion of the file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

百思不得你姐 2024-11-22 02:47:12

因此,您可以使用 Vineet Reynolds 在他的评论中建议的方法。

如果这两个进程实际上只是同一应用程序中的单独线程,那么您可以在某处设置一个标志来指示文件已打开。

如果它是两个独立的应用程序/进程,则底层文件系统应该锁定文件。当您从输出流中收到 I/O 错误时,您应该能够在该错误周围包装一个 try/catch 块,然后将您的应用程序设置为稍后重试,或者设置您的特定应用程序所需的任何行为。

文件并不是真正设计为由多个应用程序同时写入的。如果您可以描述为什么要从多个进程同时写入文件,则可能会建议其他解决方案。


最近编辑后的更新:
好的,所以您至少需要 3 个文件才能完成您所说的操作。您绝对不能尝试同时读取/写入数据到单个文件。您的三个文件是:

  1. ProcessA 将新/传入数据转储到
  2. ProcessB 当前正在处理的文件
  3. 的文件保存 ProcessB 输出的最终“输出”文件。

ProcessB 的循环:

  • 获取 file#2 中的任何数据,处理它,并将输出写入 file#3
  • 删除 file#2
  • 重复

ProcessA 的循环:

  • 将所有新的传入数据写入 file#1
  • 定期检查 file#2 是否存在
  • 何时file#2 被 ProcessB 删除,那么 ProcessA 应该停止写入 file#1,将 file#1 重命名为 file#2
  • 开始一个新的 file#1
  • 重复

So, you could use the method that Vineet Reynolds suggests in his comment.

If the two processes are actually just separate Threads within the same application, then you could set a flag somewhere to indicate that the file is open.

If it's two separate applications/processes altogether, the underlying filesystem should lock the files. When you get an I/O error from your output stream, you should be able to wrap a try/catch block around that, and then set your app up to retry later, or whatever the desired behavior is for your particular application.

Files aren't really designed to be written to simultaneously by multiple applications. If you can maybe describe why you want to write to a file simultaneously from multiple processes, there may be other solutions that could be suggested.


Updates after your recent edits:
Ok, so you need at least 3 files to do what you're talking about. You definitely cannot try to read/write data to a single file concurrently. Your three files are:

  1. the file that ProcessA dumps new/incoming data to
  2. the file that ProcessB is currently working on
  3. a final "output" file that holds the output from ProcessB.

ProcessB's loop:

  • Take any data in file#2, process it, and write the output to file#3
  • Delete file#2
  • Repeat

ProcessA's loop:

  • Write all new, incoming data to file#1
  • Periodically check to see if file#2 exists
  • When file#2 is deleted by ProcessB, then ProcessA should stop writing to file#1, rename file#1 to be file#2
  • Start a new file#1
  • Repeat
甚是思念 2024-11-22 02:47:12

如果这是两个单独的应用程序尝试访问该文件。这个人会通过 IOException 因为他无法访问它。如果发生这种情况,请在 catch(IOException err){} 中添加代码以将当前线程暂停几毫秒,然后递归地尝试再次写入 - 直到它获得访问权限。

public boolean writeFile()
{
    try
    {
       //write to file here
        return true;
    }
    catch (IOException err) // Can't access
    {
        try
        {
            Thread.sleep(200); // Sleep a bit
            return writeFile(); // Try again
        }
        catch (InterruptedException err2)
        {
           return writeFile(); // Could not sleep, try again anyway
        }
    }
}

这将继续尝试,直到您收到 StackOverflow 异常,这意味着它陷入得太深;但在这种情况下发生这种情况的可能性很小 - 只有当文件被其他应用程序保持打开很长时间时才会发生。

希望这有帮助!

If this is two separate applications trying to access the file. The one would through an IOException because he cant access it. If that occurs, in the catch(IOException err){} add code to pause the current thread for a few milliseconds and then recursively try to write again - until it gains access.

public boolean writeFile()
{
    try
    {
       //write to file here
        return true;
    }
    catch (IOException err) // Can't access
    {
        try
        {
            Thread.sleep(200); // Sleep a bit
            return writeFile(); // Try again
        }
        catch (InterruptedException err2)
        {
           return writeFile(); // Could not sleep, try again anyway
        }
    }
}

This will keep on trying until you get a StackOverflow Exception meaning that it went in too deep; but the chance of that happening in this situation is very little - would only happen if the file was to be kept open for really long time by other application.

Hope this helps!

云柯 2024-11-22 02:47:12

更新后的问题中的代码很可能是进程 B 的代码,而不是进程 A 的代码。我假设情况就是如此。

考虑到抛出了 OverlappingFileLockException 异常的实例,看来同一进程中的另一个线程正在尝试锁定同一文件。这不是 A 和 B 之间的冲突,而是 B 内部的冲突,如果查看有关 lock() 方法的 API 文档以及抛出 OverlappingFileLockException 的条件:

如果锁与请求的锁重叠
区域已被该 Java 占用
虚拟机,或者如果另一个线程
已在此方法中被阻止并且
正在尝试锁定重叠的
同一文件的区域

唯一解决方案是阻止 B 中的任何其他线程获取同一文件或文件中同一重叠区域的锁。

抛出的 IOException 有一些更有趣的消息。它可能证实了上述理论,但如果不查看整个源代码,我无法确认任何事情。 lock 方法预计会阻塞,直到获取独占锁。如果获取到了,那么写入文件应该没有问题。除了一个条件。如果文件已由同一 JVM 在不同线程中使用 File 对象(或者换句话说,第二个/不同的文件描述符)打开(并锁定),则对第一个文件描述符的尝试写入将失败,甚至如果获取了锁(毕竟,该锁不会锁定其他线程)。

一种改进的设计是,每个进程中都有一个线程,仅在一定时间内获取文件上的独占锁(当使用单个文件对象或单个文件描述符时),在该文件中执行所需的活动。文件,然后释放锁。

The code in the updated question is most likely that of process B, and not of process A. I'll assume that this is the case.

Considering that an instance of the OverlappingFileLockException exception is thrown, it appears that another thread in the same process is attempting to lock on the same file. This is not a conflict between A and B, but rather a conflict within B, if one goes by the API documentation on the lock() method and when the condition under which it throws OverlappingFileLockException:

If a lock that overlaps the requested
region is already held by this Java
virtual machine, or if another thread
is already blocked in this method and
is attempting to lock an overlapping
region of the same file

The only solution to prevent this, is to have any other thread in B prevented from acquiring a lock on the same file, or the same overlapping region in the file.

The IOException being thrown has a bit more interesting message. It probably confirms the above theory, but without looking at the entire source code, I cannot confirm anything. The lock method is expected to block until the exclusive lock is acquired. If it was acquired, then there ought to be no problem in writing to the file. Except for one condition. If the file has already been opened (and locked) by the same JVM in a different thread, using a File object (or in other words, a second/different file descriptor), then the attempted write on the first file descriptor will fail even if the lock was acquired (after all, the lock does not lock out other threads).

An improved design, would be to have a single thread in each process that acquires an exclusive lock on the file (while using a single File object, or a single file descriptor) for only a certain amount of time, perform the required activity in the file, and then release the lock.

以为你会在 2024-11-22 02:47:12

使用 MapReduce 思维来思考这个问题。我们假设每个程序都在写入输出而不读取另一个程序的输出。我会编写两个单独的文件,然后进行“减少”阶段。您的减少可能是一个简单的按时间顺序的合并。

但是,如果您的程序需要彼此的输出。您遇到了一个非常不同的问题,需要重新考虑如何划分工作。

最后,如果两个程序的输出相似但独立,并且您将其写入一个文件以便第三个程序可以读取全部内容,请考虑更改第三个程序以读取这两个文件。

Think about this using the MapReduce mentality. Let's assume the each program is writing output without reading the other's output. I would write two separate files, and then have a 'reduce' phase. Your reduction might be a simple chronologically ordered merge.

If, however, your programs' require one-anothers' output. You have a very different problem and need to rethink how you are partitioning the work.

Finally, if the two programs' outputs are similar but independent, and you are writing it into one file so a third program can read it all, consider changing the third program to read both files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文