I am using Java to develop an application, it needs to manage the file on the computer. The application have the ability/function to delete the file on the system. But I want to check whether the selected file is using/reading by another application or not first. Because I don't want to delete the file which is reading/using. How can I do so?
A:
On Windows, you can't delete files which are in use ("locked"). Java itself doesn't offer an API to check.
Aaron Digulla
2009-09-23 14:54:59
+2
A:
Could you use http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/FileChannel.html#tryLock() ?
JeeBee
2009-09-23 14:55:48
A:
If another application is using the file or actively reading it, then provided that application has done its job correctly (opening the file with a read lock), you won't be able to delete the file -- you'll get an IOException (specifically, a sharing violation). Catch the exception to know whether there was a problem.
T.J. Crowder
2009-09-23 14:56:14