Check if file is already open

前端 未结 8 1270
悲&欢浪女
悲&欢浪女 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 15:11

        //  TO CHECK WHETHER A FILE IS OPENED 
        //  OR NOT (not for .txt files)
    
        //  the file we want to check
        String fileName = "C:\\Text.xlsx";
        File file = new File(fileName);
    
        // try to rename the file with the same name
        File sameFileName = new File(fileName);
    
        if(file.renameTo(sameFileName)){
            // if the file is renamed
            System.out.println("file is closed");    
        }else{
            // if the file didnt accept the renaming operation
            System.out.println("file is opened");
        }
    

提交回复
热议问题