tags:

views:

60

answers:

3

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
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