(Java)锁定后仍可读的文件
我目前正试图用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);
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有效
非常感谢我做了一项研究:
File File=新文件(“File.txt”)代码>
boolean fileisnotlock=file.renameTo(文件)代码>这是一种检查文件是否在Windows上锁定的简单方法
操作为止代码>
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();
我做了一项研究:
File File=新文件(“File.txt”)代码>
boolean fileisnotlock=file.renameTo(文件)代码>这是一种检查文件是否在Windows上锁定的简单方法
操作为止代码>
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作为显示函数调用的方式。