I have an utility which has several threads running concurrently. One thread adds a file from a directory to a BlockingQueue for further processing by another
Thread.
Now the problem is when a new file takes a more time than usual to be copied, one exception is thrown by the underline thread.
java.io.FileNotFoundException: C:\online\inputDir\file.txt (The process cannot access the file because it is being used by another process)
And this attemt to add the file to the queue fails, next thread picks the file normally is it is copied properly, otherwise same exception is thrown. I want to know
if there is any way to know that a file being used by another process. I tried canRead(), canWrite() and canExecute() of the File.class, but they always return true.[Though these methods are user specific].
I am reading and writing this way :
BufferedInputStream inputStream=null;
BufferedOutputStream outputStream=null;
try{
inputStream= new BufferedInputStream(new FileInputStream(file));
outputStream= new BufferedOutputStream(new FileOutputStream(processingDirectoryPath+File.separator+file.getName()));
byte[] buff = new byte[2048];
int bit;
while((bit=inputStream.read(buff))>0){
outputStream.write(buff,0,bit);}
outputStream.flush();
}
catch(....
Can we make a new type of FilterInputStream which wiil make sure that only when a file is copied, after that an attempt to read or write on that file will start, and chain it to the existing input or output streams.