How to check if a file is open by another process (Java/Linux)?

前端 未结 5 733
抹茶落季
抹茶落季 2020-12-08 14:30

I am trying to check if a certain java.io.File is open by an external program. On windows I use this simple trick:

try {
    FileOutputStream fos = new FileO         


        
5条回答
  •  臣服心动
    2020-12-08 14:37

    You can run from Java program the lsof Unix utility that tells you which process is using a file, then analyse its output. To run a program from Java code, use, for example, Runtime, Process, ProcessBuilder classes. Note: your Java program won't be portable in this case, contradicting the portability concept, so think twice whether you really need this :)

提交回复
热议问题