Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Java)锁定后仍可读的文件_Java_File_Locking - Fatal编程技术网

(Java)锁定后仍可读的文件

(Java)锁定后仍可读的文件,java,file,locking,Java,File,Locking,我目前正试图用Java锁定一个文件,以防止其他方法使用它 File someFile = new File(somePath); FileLock lock = null; FileChannel fromFileChannel = null; FileOutputStream fromFileStream = null; try { someFile.createNewFile(); writeStringToFile(randomString, someFile);

我目前正试图用Java锁定一个文件,以防止其他方法使用它

File someFile = new File(somePath);

FileLock lock = null;
FileChannel fromFileChannel = null;
FileOutputStream fromFileStream = null;
try {
    someFile.createNewFile();
    writeStringToFile(randomString, someFile);

    fromFileStream = new FileOutputStream(someFile);
    fromFileChannel = fromFileStream.getChannel();

    lock = fromFileChannel.lock(0L, Long.MAX_VALUE, false);
} catch (IOException e) {
    fail();
}

System.out.println(someFile.canRead());
System.out.println(someFile.canWrite());
但为什么canRead和canWrite都返回真值?锁是否仅对FileChannel有效


非常感谢

我做了一项研究:

  • 这种行为对于Windows是不正确的,因为File.canWrite()只检查MS-DOS只读标志,而不检查文件的ACL。因此,它将为您无权写入的文件提供误报

  • File File=新文件(“File.txt”)
    
    boolean fileisnotlock=file.renameTo(文件)这是一种检查文件是否在Windows上锁定的简单方法

  • 您的锁一直工作到您从FileChannel.close()或lock.release()执行
    操作为止

  • 我尝试运行两个线程。第一个打开fileChannel并锁定它并写入。在第二个线程中,如果您尝试再次锁定文件,您将获得
    OverlappingFileLockException
    ,或者如果您尝试写入文件,您将获得另一个进程阻止访问的
    IOException
    。若你们真的解除了对线程1的锁定,你们可以在不同的线程中自由编辑文件

  • 因此,它看起来非常棘手,如果你想使用它们,它必须是专为锁设计的

    Thread thread1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        FileLock lock = null; FileChannel fromFileChannel = null; FileOutputStream fromFileStream = null;
                        fromFileStream = new FileOutputStream(fileToCreatePath.toFile());
                        fromFileChannel = fromFileStream.getChannel();
    
                        lock = fromFileChannel.lock(0L, Long.MAX_VALUE, false);
                        ByteBuffer buff = ByteBuffer.wrap("Hello from thread 1".getBytes(StandardCharsets.UTF_8));
                        fromFileChannel.write(buff);
                        //fromFileChannel.close();  - Close channel or release lock
                        // lock.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
            });
            thread1.start();
    
    Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(100);
                        FileChannel fromFileChannel = null; FileOutputStream fromFileStream = null; FileLock lock = null;
                        fromFileStream = new FileOutputStream(fileToCreatePath.toFile());
                        fromFileChannel = fromFileStream.getChannel();
                      //  lock = fromFileChannel.tryLock(); - FileLockException
                        ByteBuffer buff = ByteBuffer.wrap("Hello from thread 2 :(".getBytes(StandardCharsets.UTF_8));
                        fromFileChannel.write(buff); // IOException
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread2.start();
    

    我做了一项研究:

  • 这种行为对于Windows是不正确的,因为File.canWrite()只检查MS-DOS只读标志,而不检查文件的ACL。因此,它将为您无权写入的文件提供误报

  • File File=新文件(“File.txt”)
    
    boolean fileisnotlock=file.renameTo(文件)这是一种检查文件是否在Windows上锁定的简单方法

  • 您的锁一直工作到您从FileChannel.close()或lock.release()执行
    操作为止

  • 我尝试运行两个线程。第一个打开fileChannel并锁定它并写入。在第二个线程中,如果您尝试再次锁定文件,您将获得
    OverlappingFileLockException
    ,或者如果您尝试写入文件,您将获得另一个进程阻止访问的
    IOException
    。若你们真的解除了对线程1的锁定,你们可以在不同的线程中自由编辑文件

  • 因此,它看起来非常棘手,如果你想使用它们,它必须是专为锁设计的

    Thread thread1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        FileLock lock = null; FileChannel fromFileChannel = null; FileOutputStream fromFileStream = null;
                        fromFileStream = new FileOutputStream(fileToCreatePath.toFile());
                        fromFileChannel = fromFileStream.getChannel();
    
                        lock = fromFileChannel.lock(0L, Long.MAX_VALUE, false);
                        ByteBuffer buff = ByteBuffer.wrap("Hello from thread 1".getBytes(StandardCharsets.UTF_8));
                        fromFileChannel.write(buff);
                        //fromFileChannel.close();  - Close channel or release lock
                        // lock.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
            });
            thread1.start();
    
    Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(100);
                        FileChannel fromFileChannel = null; FileOutputStream fromFileStream = null; FileLock lock = null;
                        fromFileStream = new FileOutputStream(fileToCreatePath.toFile());
                        fromFileChannel = fromFileStream.getChannel();
                      //  lock = fromFileChannel.tryLock(); - FileLockException
                        ByteBuffer buff = ByteBuffer.wrap("Hello from thread 2 :(".getBytes(StandardCharsets.UTF_8));
                        fromFileChannel.write(buff); // IOException
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread2.start();
    

    阅读文档时,我认为如果无法获取锁,则调用
    lock()
    会引发异常。因此,如果您获得了锁,您的锁总是
    canRead
    canWrite
    。@markspace在这种情况下它不会(它从不进入catch块,lock()只抛出IOExceptions),因此锁被获取。阅读文档,我认为
    lock()
    调用在无法获取锁时抛出异常。因此,如果您获得了锁,您的锁总是
    canRead
    canWrite
    。@markspace在这种情况下它不会这样做(它从不进入catch块,lock()只抛出IOExceptions),因此锁被获得。问题是,它们可以。我尝试使用canRead和canWrite作为显示函数调用的方式。问题是,它们可以。我尝试使用canRead和canWrite作为显示函数调用的方式。