Check if file is already open

前端 未结 8 1194
悲&欢浪女
悲&欢浪女 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:54

    org.apache.commons.io.FileUtils.touch(yourFile) doesn't check if your file is open or not. Instead, it changes the timestamp of the file to the current time.

    I used IOException and it works just fine:

    try 
    {
      String filePath = "C:\sheet.xlsx";
      FileWriter fw = new FileWriter(filePath );                
    }
    catch (IOException e)
    {
        System.out.println("File is open");
    }
    
    0 讨论(0)
  • 2020-11-22 14:56

    Hi I really hope this helps.

    I tried all the options before and none really work on Windows. The only think that helped me accomplish this was trying to move the file. Event to the same place under an ATOMIC_MOVE. If the file is being written by another program or Java thread, this definitely will produce an Exception.

     try{
    
          Files.move(Paths.get(currentFile.getPath()), 
                   Paths.get(currentFile.getPath()), StandardCopyOption.ATOMIC_MOVE);
    
          // DO YOUR STUFF HERE SINCE IT IS NOT BEING WRITTEN BY ANOTHER PROGRAM
    
     } catch (Exception e){
    
          // DO NOT WRITE THEN SINCE THE FILE IS BEING WRITTEN BY ANOTHER PROGRAM
    
     }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-11-22 15:07

    If file is in use FileOutputStream fileOutputStream = new FileOutputStream(file); returns java.io.FileNotFoundException with 'The process cannot access the file because it is being used by another process' in the exception message.

    0 讨论(0)
  • 2020-11-22 15:10

    On Windows I found the answer https://stackoverflow.com/a/13706972/3014879 using

    fileIsLocked = !file.renameTo(file)

    most useful, as it avoids false positives when processing write protected (or readonly) files.

    0 讨论(0)
  • 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");
        }
    
    0 讨论(0)
提交回复
热议问题