如何在文件上设置写锁定 [英] How to put write lock on file
本文介绍了如何在文件上设置写锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在服务器上有文本文件.
用户可以对其进行读写.
我想在第一个用户进入并打开文件时在文件上放写锁,
如果另一个用户介于两者之间并打开文件,则他应该只读取该文件,不允许写入文件.
I have text file on server.
User can read it and write in it.
I want to put write lock on file when first user come and open it ,
If another user comes in between and open the file he should just read the file not allow to write in file.
推荐答案
最简单的方法:
使用自定义对象"FileWithLock"而不是"File".您只需在那一个上设置一个锁并做出反应即可.
simplest approach:
Use a custom object "FileWithLock" instead of "File". You can simply set a lock on that one and react.
import java.io.File;
public class FileWithLock extends File
{
private boolean locked = false;
public FileWithLock(String pathName){
super(pathName);
}
public void setLocked(boolean locked){
this.locked = locked;
}
public boolean bisLocked(){
return locked;
}
}
这篇关于如何在文件上设置写锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文