[英]Removing file locks in Windows and Java
I have a Java program that opens a file using the RandomAccessFile class. 我有一个Java程序,可以使用RandomAccessFile类打开文件。
I'd like to be able to rename that file while it is opened by Java. 我希望能够在Java打开文件时重命名该文件。 In Unix, this isn't a problem. 在Unix中,这不是问题。 Does anyone know how I can do this in Windows? 有谁知道我如何在Windows中做到这一点?
Should I set Java to open it a certain way? 我应该设置Java以某种方式打开它吗?
Thanks in advance. 提前致谢。
Edit, clarification: I'd like to be able to set this file sharing access in my Java code. 编辑,澄清:我希望能够在Java代码中设置此文件共享访问权限。
You need to close any handles on the file at any way. 您需要以任何方式关闭文件上的所有句柄。 Eg a FileOutputStream
on that File
must be closed before you can rename it. 例如,在重命名该File
之前,必须先关闭该File
上的FileOutputStream
。 That's just the way how the Windows file system works. 这就是Windows文件系统的工作方式。
in_stream.close();
out_stream.getChannel().close();
out_stream.close();
Anyhow in Windows it stay locked :*( 无论如何,在Windows中它保持锁定状态:*(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.