title: java - The process cannot access the file because it is being used by another process
body: <p>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 and then the file is deleted. </p>
  
  <p>This works when I make a single request but as soon as I subject my code to 5 or 10 user request from jMeter, the contents are published on kafka successfully but the code isn't able to delete the file. I get a <code>FileSystemException</code> with a message that <code>The process cannot access the file because it is being used by another process.</code>.</p>
  
  <p>I guess there is some concurrency issue which I am unable to see.</p>
  
  <pre><code>public void monitor() throws IOException, InterruptedException {
      Path faxFolder = Paths.get(TEMP_FILE_LOCATION);
      WatchService watchService = FileSystems.getDefault().newWatchService();
      faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
      boolean valid = true;
      do {
          WatchKey watchKey = watchService.take();
          for (WatchEvent&lt;?&gt; event : watchKey.pollEvents()) {
              if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
                  String fileName = event.context().toString();
                  publishToKafka(new File(TEMP_FILE_LOCATION + fileName).toPath(), "topic");
              }
          }
          valid = watchKey.reset();
      } while (valid);
  }
  
  private void publishToKafka(Path path, String topic) {
      try (BufferedReader reader = Files.newBufferedReader(path)) {
          String input = null;
          while ((input = reader.readLine()) != null) {
              kafkaProducer.publishMessageOnTopic(input, topic);
          }
      } catch (IOException e) {
          LOG.error("Could not read buffered file to send message on kafka.", e);
      } finally {
          try {
              Files.deleteIfExists(path); // This is where I get the exception
          } catch (IOException e) {
              LOG.error("Problem in deleting the buffered file {}.", path.getFileName(), e);
          }
      }
  }
  </code></pre>
  
  <p>Exception Log : </p>
  
  <pre><code>java.nio.file.FileSystemException: D:\upload\notif-1479974962595.csv: The process cannot access the file because it is being used by another process.
  
      at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
      at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
      at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
      at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
      at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source)
      at java.nio.file.Files.deleteIfExists(Unknown Source)
      at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.publishToKafka(MonitorDirectory.java:193)
      at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.sendData(MonitorDirectory.java:125)
      at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.run(MonitorDirectory.java:113)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>