如何locking文件以避免使用NIO软件包进行删除

我正在使用NIO文件通道来pipe理文件,并locking它们。 到目前为止,它的工作,但是当我locking文件使用NIO文件locking它locking该文件,所以文件内容不能改变。 例如,如果我尝试编辑记事本上的文本文件它会告诉我以下错误信息: 文件锁定

这是预期的结果,但是,如果我试图从Windows资源pipe理器中删除文件(我没有testing过其他操作系统可能也将是可能的),它会允许我,这是不受欢迎的,我想知道如果可以打开一个文件句柄

使用的代码:

private static final byte[] MessageBytes; static { byte tmp[]; try { tmp = "Hello World".getBytes("UTF-8"); } catch (UnsupportedEncodingException ex) { //if fail get the bytes in whatever the java VM charset sets as default tmp = "Hello World".getBytes(); } MessageBytes = tmp; } private static final String Filename = "Lock_test.txt"; private static void createFileandLock() { Path FilePath = Paths.get(Filename); FileChannel OpenFCh; try { OpenFCh = FileChannel.open(FilePath, StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE // ,StandardOpenOption.APPEND ); System.out.println("File Channel is Open."); } catch (IOException err) { OpenFCh = null; } if (OpenFCh != null) { FileLock Lock = null; try { Lock = OpenFCh.lock(); } catch (IOException err) { System.out.println("Unable To Lock the File."); } try { OpenFCh.write(ByteBuffer.wrap(MessageBytes)); OpenFCh.force(false); System.out.println("Message Recorded"); } catch (IOException ex) { System.out.println("Unable To write data into file"); } try { // at this point file still locked and open. //lets wait for input and meanwhile ask to delete the file. System.out.print("Please Try to delete file at: "); System.out.println(FilePath.toString()); System.out.println("Press Enter to Continue"); System.in.read(); } catch (IOException ex) { } if (Lock != null) { try { Lock.close(); } catch (IOException ex) { } } try { OpenFCh.close(); } catch (IOException ex) { } } } 

进一步研究后,我注意到,使用RandomAccessFile将locking文件,避免删除,因为它创build一个文件描述符 ,基本上打开下划线操作系统上的句柄。 所以使用RAF确实能提供理想的结果: 文件打开消息

使用的代码:

 private static void createRAFileandLock() { RandomAccessFile RAf; try { RAf = new RandomAccessFile(Filename, "rw"); } catch (FileNotFoundException ex) { //since is open as RW shold not trigger. RAf = null; } if (RAf != null) { FileChannel OpenFCh = RAf.getChannel(); FileLock Lock = null; try { Lock = OpenFCh.lock(); } catch (IOException err) { System.out.println("Unable To Lock the File."); } try { OpenFCh.write(ByteBuffer.wrap(MessageBytes)); OpenFCh.force(false); System.out.println("Message Recorded"); } catch (IOException ex) { System.out.println("Unable To write data into file"); } // at this point file still locked and open. //lets wait for input and meanwhile ask to delete the file. try { System.out.print("Please Try to delete file at: "); System.out.println(Filename); System.out.println("Press Enter to Continue"); System.in.read(); } catch (IOException ex) { } if (Lock != null) { try { Lock.close(); } catch (IOException ex) { } } try { OpenFCh.close(); RAf.close(); } catch (IOException ex) { } } } 

不过,我想知道是否有可能只使用NIO进行存档。 随机访问文件在IO包上。

没有指定FileLock来防止删除。 它只是指定与其他文件锁交互,所以你已经深入到依赖于平台的行为。 如果RandomAccessFile以某种方式做你想要的,你可能会被卡住,但你不能依靠它。

注意当然FileChannel.open()使用FileDescriptor,句柄等