javamultithreadingapache-commons-io

Directory Listener z Commons IO - action can't be completed


I wrote an application that listens to a given folder, then logs events and writes to the database (action type, file name, content, date). I based the applications on the Producer Consumer pattern and used the ArrayBlockingQueue.

I have a problem of this type, that when I add a file in this folder and later I want to delete or modify it (this applies only to the first file created in this folder) it pops up

error

This is one thing, the other one that I would like to skip while (in some way) in DbWriter and I have no idea how to do it.

Thanks for all the answers


Solution

  • You can not delete the file because inFileProcessor.getContent, you create a FileReader and did not close it, this will cause the JVM lock the file. To solve this problem, just close the FileReader after using, like this :

    public static String getContent (File file) throws IOException {
        FileReader fileReader = new FileReader(file);
        String content = IOUtils.toString(fileReader);
        fileReader.close();
        return content;
    }