Check if file is already open

前端 未结 8 1315
悲&欢浪女
悲&欢浪女 2020-11-22 14:32

I need to write a custom batch File renamer. I\'ve got the bulk of it done except I can\'t figure out how to check if a file is already open. I\'m just using the java.

8条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 14:57

    Using the 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
    }
    

提交回复
热议问题