[Java]如何判断文件是否正在使用?

时间:2022-11-23 09:26:43

How to determine whether a file is using?

如何确定文件是否正在使用?

5 个解决方案

#1


In java you can lock Files and checking for shared access.

在java中,您可以锁定文件并检查共享访问。

You can use a file lock to restrict access to a file from multiple processes

您可以使用文件锁来限制从多个进程访问文件

public class Locking {
   public static void main(String arsg[])
       throws IOException {
     RandomAccessFile raf =
       new RandomAccessFile("junk.dat", "rw");
     FileChannel channel = raf.getChannel();
     FileLock lock = channel.lock();
     try {
       System.out.println("Got lock!!!");
       System.out.println("Press ENTER to continue");
       System.in.read(new byte[10]);
     } finally {
       lock.release();
     }
   }
}

You also can check whether a lock exists by calling

您还可以通过调用来检查锁是否存在

// Try acquiring the lock without blocking. This method returns
// null or throws an exception if the file is already locked.
        try {
            lock = channel.tryLock();
        } catch (OverlappingFileLockException e) {
            // File is already locked in this thread or virtual machine
        }

#2


This question is about C# but i think it mostly applies to Java as well

这个问题是关于C#但我认为它也主要适用于Java

#3


I don't think that Java can tell you whether another process is using a file. Depending on what you're trying to do, you might get an IOException when you try to manipulate it. Otherwise, if you're no Linux, you might want to look at lsof.

我不认为Java可以告诉您另一个进程是否正在使用文件。根据您尝试执行的操作,当您尝试操作它时,可能会收到IOException。否则,如果你不是Linux,你可能想看看lsof。

#4


I think that what you're asking about is whether the file you're attempting to use is locked. You can use the FileLock class to attempt to lock the file you're interested in. This class is intended to map to the native file-locking facility on the file system being used. If you can lock the file, it's safe to assume that no other process holds a lock on the file.

我想你所问的是你试图使用的文件是否被锁定。您可以使用FileLock类来尝试锁定您感兴趣的文件。此类旨在映射到正在使用的文件系统上的本机文件锁定工具。如果您可以锁定文件,则可以安全地假设没有其他进程持有文件锁。

#5


Here is an easy Solution for Windows Users: http://www.dr-hoiby.com/WhoLockMe/

这是一个针对Windows用户的简单解决方案:http://www.dr-hoiby.com/WhoLockMe/

Tiny Tool but useful...

小工具但很有用......

#1


In java you can lock Files and checking for shared access.

在java中,您可以锁定文件并检查共享访问。

You can use a file lock to restrict access to a file from multiple processes

您可以使用文件锁来限制从多个进程访问文件

public class Locking {
   public static void main(String arsg[])
       throws IOException {
     RandomAccessFile raf =
       new RandomAccessFile("junk.dat", "rw");
     FileChannel channel = raf.getChannel();
     FileLock lock = channel.lock();
     try {
       System.out.println("Got lock!!!");
       System.out.println("Press ENTER to continue");
       System.in.read(new byte[10]);
     } finally {
       lock.release();
     }
   }
}

You also can check whether a lock exists by calling

您还可以通过调用来检查锁是否存在

// Try acquiring the lock without blocking. This method returns
// null or throws an exception if the file is already locked.
        try {
            lock = channel.tryLock();
        } catch (OverlappingFileLockException e) {
            // File is already locked in this thread or virtual machine
        }

#2


This question is about C# but i think it mostly applies to Java as well

这个问题是关于C#但我认为它也主要适用于Java

#3


I don't think that Java can tell you whether another process is using a file. Depending on what you're trying to do, you might get an IOException when you try to manipulate it. Otherwise, if you're no Linux, you might want to look at lsof.

我不认为Java可以告诉您另一个进程是否正在使用文件。根据您尝试执行的操作,当您尝试操作它时,可能会收到IOException。否则,如果你不是Linux,你可能想看看lsof。

#4


I think that what you're asking about is whether the file you're attempting to use is locked. You can use the FileLock class to attempt to lock the file you're interested in. This class is intended to map to the native file-locking facility on the file system being used. If you can lock the file, it's safe to assume that no other process holds a lock on the file.

我想你所问的是你试图使用的文件是否被锁定。您可以使用FileLock类来尝试锁定您感兴趣的文件。此类旨在映射到正在使用的文件系统上的本机文件锁定工具。如果您可以锁定文件,则可以安全地假设没有其他进程持有文件锁。

#5


Here is an easy Solution for Windows Users: http://www.dr-hoiby.com/WhoLockMe/

这是一个针对Windows用户的简单解决方案:http://www.dr-hoiby.com/WhoLockMe/

Tiny Tool but useful...

小工具但很有用......