Redian新闻
>
Talk a little more about How to lock a file
avatar
Talk a little more about How to lock a file# Java - 爪哇娇娃
d*o
1
Thank you very much for all the discussion here!
I tried different ways and there is no perfect solution so
far.
1. I'd like to bring up another issue here:
The reason I want to lock a file is to prevent any other
access. But the other program has to know when the file is
released. In another word, other program has to know if the
file is being used. Let me make an example here:
In COM programming, after we create an intance of a COM, we
can call AddRef and Release to check how many instances
avatar
i*e
2
You might have two ways to do it:
(1) use synchronization, synchronize on a common object,
like this,
synchronized( commonObject ) {
access the file;
}
or
(2) use a lock file , say myfile.lck
if ( lock file exits ) quit;
else {
create the lock file myfile.lck;
if (creation is successful) {
access the file;
remove the myfile.lck;
avatar
m*t
3
ditto, are you saying it is actually a native legacy system
that would be writing the file? If that's the case, and you have
zero control on that system, the only solution I can
come up w/ is to write a native program to detect activities
on the file. You can call Win32 API to find out the process of
the legacy system which is holding the file handle, and keep
polling it until that file handle is released by the process.
This is, of course, based on an assumption that the legacy process
would
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。