文件修改始终是一件很麻烦也很出错的行为。多线程读写文件更是会加大文件内容混乱的几率,这时,一定要保证在某一个时刻,只有一个线程在对文件进行读写操作。那么其他访问文件的线程该怎么办呢?就像去ATM取钱一样,当ATM正在被使用时,那么其他想要使用ATM的人只能等待知道ATM能使用。
读写文件也一样,当一个线程获得文件时,给予这个线程文件锁。只有拥有文件锁的线程才能操作文件,其他线程就需要一直等待,直到获得文件锁。
下面的代码实现了如下的功能:
需要注意的地方有:
private void addConfig(String configStr){
File file = new File(CONFIG_FILE);
RandomAccessFile write = null;
FileChannel channel = null;
FileLock lock = null;
try {
write = new RandomAccessFile(file, "rws");
channel = write.getChannel();
while(true){
try {
lock = channel.lock();//尝试获得文件锁,若文件正在被使用,则一直等待
break;
} catch (Exception e) {
System.out.println("write::: some other thread is holding the file...");
}
}
String content = readConfig(write, configStr);
write.seek(0);//替换原有文件内容
write.write(content.getBytes());
lock.release();
channel.close();
write.close();
} catch (IOException e) {
throw new FileNotExistException("config file is not exist").addScene("config", CONFIG_FILE);
}
}
readConfig(RandomAccessFile write , String configStr)
方法会读取文件内容,并将字符串
configStr插入其中。其实现如下:
private String readConfig(RandomAccessFile reader, String configStr) {
StringBuffer sb = new StringBuffer();
try {
if (reader != null) {
String txt = new String();
while ((txt = reader.readLine()) != null) {
sb.append(txt + "\n");
if (" \"collect_items\":[".equals(txt)) {
sb.append(configStr);
}
}
} else {
throw new FileIOException("reader is null...").addScene(
"reader", reader);
}
return sb.toString();
} catch (IOException e) {
throw new FileIOException("exception when read content").addScene(
"config", CONFIG_FILE);
}
}
write.seek(0);
这个方法来实现。此时写入的内容就会覆盖掉原来文件中的内容。
while(true){
try {
lock = channel.lock();
break;
} catch (Exception e) {
System.out.println("write::: some other thread is holding the file...");
}
}