Check if file is already open

前端 未结 8 1313
悲&欢浪女
悲&欢浪女 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");
    }
    

提交回复
热议问题