Question : FileInputStream check if a file is in use

I tried to open a file that's being downloaded using FileInputStream. But it gives me the following errors:
java.io.FileNotFoundException: C:\\tmp\jre-7-ea-bin-b93-windows-i586-13_may_2010.jar (The process cannot access the file because it is being used by another process)
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.<init>(FileInputStream.java:137)
      at java.io.FileInputStream.<init>(FileInputStream.java:96)
      ...

I did try to use File file = new File(NewFile);
and put if (file.canRead()) before I use the FileInputStream. But teh canRead( ) returns true while the FileInputStream failed with above exception.

How can I check if the FileInputStream will work?

Thanks!

Answer : FileInputStream check if a file is in use

when different processes access the file, i think you can use
FileChannel.lock()/FileChannel.tryLock(),
this will obtain a exclusive lock on the File for the JVM process, so other process (download) cannot access it and the other way around.





Random Solutions  
 
programming4us programming4us