简体   繁体   English

我如何得知其他应用程序正在打开文件?

[英]How can I tell wheter a file is opened by an other application?

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

Command fuser -v filename will tell you everything you need to know: 命令fuser -v filename将告诉您所有您需要了解的内容:

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

On windows you could download handle which is a command line tool to identify which windows file handles are owned by which processes. 在Windows上,您可以下载handle ,它是一个命令行工具,用于识别哪些进程拥有Windows文件句柄。

Output looks like this: 输出看起来像这样:

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...)

Here an example application which uses handle.exe to determine if there is a handle on a file or directory (Windows only): 这是一个使用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);
        }
    }
}

You probably want to use file locking. 您可能要使用文件锁定。

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

Edit: This is presuming you mean programatically with PHP or Java. 编辑:这是假定您是用PHP或Java编程表示。

On linux you can scan through all the /proc/{pid}/fd/nnn file descriptors to see if the file in question is already open. 在Linux上,您可以浏览所有/ proc / {pid} / fd / nnn文件描述符,以查看问题文件是否已打开。

Using files to share data between running programs is generally a bad idea and error prone. 使用文件在正在运行的程序之间共享数据通常是一个坏主意,并且容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何告诉数据库某些更改是由休眠而不是其他应用程序进行的? - How can I tell my database that some changes are made by hibernate and not by other application? 当文件被其他应用程序打开或使用时如何写? - How to write a file while it's opened or used by other application? 如何轻松判断文件是否已加密 - How can I tell easily if a file is encrypted 如何判断文件是否已重命名? - How can I tell if a file was renamed? ZipException用于可以在其他程序上打开的文件 - ZipException for file that can be opened on other programs 在Java中如何解锁文件或写入由excel打开的文件 - In java how can I unlock a file or write to a file opened by excel 如何让FileWatcher告诉我修改后的文件是文件夹? - How can I get FileWatcher to tell me the file modified is a folder? 如何判断ListView中所选索引是文件还是目录? - How can I tell if the selected index in a ListView is a file or directory? 如何从 Java 应用程序中为操作系统上的所有其他进程锁定文件? - How can I lock a file from within a Java application for all other processes on a operating system? 如何获取列表(搜索)以告诉我每个列表是否都在另一个列表(主列表)中? - How can I get the List (Search) to tell me whether or not each it is in the other List (main)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM