【问题标题】:How can I tell wheter a file is opened by an other application?如何判断文件是否被另一个应用程序打开?
【发布时间】:2011-09-10 07:08:44
【问题描述】:

有没有办法判断一个文件是否被Java的PHP中的另一个应用程序打开?

【问题讨论】:

    标签: java php


    【解决方案1】:

    命令fuser -v filename 会告诉你你需要知道的一切:

    $ fuser -v test.php 
                         USER        PID ACCESS COMMAND
    test.php:            guest     17983 F.... cat
    

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      在 windows 上,您可以下载handle,这是一个命令行工具,用于识别哪些 windows 文件句柄属于哪些进程。

      输出如下所示:

      Handle v3.46
      Copyright (C) 1997-2011 Mark Russinovich
      Sysinternals - www.sysinternals.com
      
      ------------------------------------------------------------------------------
      System pid: 4 NT AUTHORITY\SYSTEM
         84: File  (R--)   C:\System Volume Information\_restore{5D536487-92AI-5I25-9237-28AHSOU23728}\RP425\change.log
         B4: File  (RWD)   C:\Documents and Settings\All Users\Application Data\avg9\Log\avgldr.log
        728: File  (-W-)   C:\pagefile.sys
        7A4: File  (---)   C:\WINDOWS\system32\config\SECURITY
        (etc...)
      

      这里是一个示例应用程序,它使用 handle.exe 来确定文件或目录上是否有句柄(仅限 Windows):

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      
      /**
       * Application which determines which processes have a handle on a file or
       * directory. Pass the file or directory to check as the first application
       * parameter.
       * 
       * This application uses handle.exe, which can be downloaded here:
       * http://technet.microsoft.com/en-us/sysinternals/bb896655
       * 
       * Copy handle.exe to C:/Program Files/handle/
       * 
       * For the Runtime.exec() code I looked at:
       * http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=2
       * 
       * @author Adriaan
       */
      public class Handle {
      
          private static final String HANDLE_PATH = "C:/Program Files/handle/handle.exe";
          private static final String DEFAULT_TARGET = "C:\\WINDOWS";
      
          public static void main(String[] args) throws IOException,
                  InterruptedException {
      
              checkOS();
              String fileName = getFileName(args);
              Process proc = executeCommand(fileName);
              readResults(fileName, proc);
              checkTermination(proc);
          }
      
          private static void checkOS() {
      
              String osName = System.getProperty("os.name");
              if (!osName.contains("Windows")) {
                  throw new IllegalStateException("Can only run under Windows");
              }
          }
      
          private static String getFileName(String[] args) {
      
              String fileName;
              if (args != null && args.length > 0) {
                  fileName = args[0];
              } else {
                  fileName = DEFAULT_TARGET;
              }
              return fileName;
          }
      
          private static Process executeCommand(String fileName) throws IOException {
      
              String[] cmd = new String[] { HANDLE_PATH, fileName };
              Runtime rt = Runtime.getRuntime();
              Process proc = rt.exec(cmd);
              return proc;
          }
      
          private static void readResults(final String fileName, final Process proc) {
      
              Thread errorHandler = new Thread() {
                  public void run() {
                      try {
                          BufferedReader br = new BufferedReader(
                                  new InputStreamReader(proc.getErrorStream()));
                          String line = null;
                          while ((line = br.readLine()) != null) {
                              System.err.println(line);
                          }
                      } catch (IOException ioe) {
                          ioe.printStackTrace();
                      }
                  }
              };
      
              Thread outputHandler = new Thread() {
                  public void run() {
                      try {
                          BufferedReader br = new BufferedReader(
                                  new InputStreamReader(proc.getInputStream()));
                          String line = null;
                          while ((line = br.readLine()) != null) {
      
                              if (line.endsWith(fileName)) {
                                  System.out.println(line);
                              }
                          }
                      } catch (IOException ioe) {
                          ioe.printStackTrace();
                      }
                  }
              };
              errorHandler.start();
              outputHandler.start();
          }
      
          private static void checkTermination(final Process proc)
                  throws InterruptedException {
              int exitVal = proc.waitFor();
              if (exitVal != 0) {
                  throw new IllegalStateException("Exitvalue " + exitVal);
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        您可能想要使用文件锁定。

        http://tuxradar.com/practicalphp/8/11/0

        编辑:这是假设您的意思是使用 PHP 或 Java 以编程方式。

        【讨论】:

          【解决方案5】:

          在 linux 上,您可以扫描所有 /proc/{pid}/fd/nnn 文件描述符以查看相关文件是否已打开。

          使用文件在正在运行的程序之间共享数据通常是个坏主意,而且容易出错。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-01-27
            • 1970-01-01
            • 2011-06-05
            • 2014-11-22
            • 2010-11-06
            • 2011-10-06
            • 1970-01-01
            • 2012-03-09
            相关资源
            最近更新 更多