java - The process cannot access the file because it is being used by another process

后端 未结 4 1357
生来不讨喜
生来不讨喜 2021-02-14 00:24

I have a piece of code that monitors a directory for addition of files. Whenever a new file is added to the directory, the contents of the file are picked and published on kafka

4条回答
  •  野性不改
    2021-02-14 01:07

    It is always a better idea to add sleep time in WatchService events:

    if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
    
           try {
                Thread.sleep(3000);
           } catch (InterruptedException e) {
                e.printStackTrace();
           }
    
    // now do your intended jobs ... 
    

    I needed to add sleep time otherwise it wouldn't work for multiple requests and used to get the error:

    The process cannot access the file because it is being used by another process
    

提交回复
热议问题