4

The issue is stemming from the specificied line of code in this try block:

try {
            fInputStream = new FileInputStream(path);
#thisLine   byteCount += IOUtils.copyLarge(fInputStream, fOutputStream);
            fileCount++;
    }

The stack-trace looks like this:

java.io.IOException: The process cannot access the file because another 
process has locked a portion 
 of the file 
        at java.io.FileInputStream.readBytes(Native Method) 
        at java.io.FileInputStream.read(FileInputStream.java:233) 
        at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1719) 
        at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1696) 

This seems to be a Windows-specific issue. Is there some File I/O best practice(s) specific to Windows that I might be missing?

knownothing
  • 57
  • 1
  • 1
  • 8

2 Answers2

1

I have resolved that issue by passing earlier created fileChannel object.

FileChannel fileChannel = FileChannel.open(path, 
                   StandardOpenOption.READ,StandardOpenOption.WRITE);

then I created InputStream object like this:

Channels.newInputStream(fileChannel);

It worked and no more exception occurred.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
0

Check if another process is accessing the same file. On Windows follow this post to figure it out -> https://superuser.com/questions/399659/how-can-i-identify-what-application-is-using-a-given-file

It should work as soon as no other process is accessing the same file.

To make sure you can actually access the file consider reading the following topic -> Java: Check if file is already open

Community
  • 1
  • 1
Julian Pieles
  • 3,880
  • 2
  • 23
  • 33