Java – check if the file is open

I need to write a custom batch file renamer I've done most of the work, except I can't figure out how to check whether the file is open I just use Java io. File package and has a canwrite () method, but it doesn't seem to test whether the file is used by other programs Any ideas on how to make this work?

Solution

Using Apache commons IO Library

boolean isFileUnlocked = false;
try {
    org.apache.commons.io.FileUtils.touch(yourFile);
    isFileUnlocked = true;
} catch (IOException e) {
    isFileUnlocked = false;
}

if(isFileUnlocked){
    // Do stuff you need to do with a file that is NOT locked.
} else {
    // Do stuff you need to do with a file that IS locked
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>