文件修改始终是一件很麻烦也很出错的行为。多线程读写文件更是会加大文件内容混乱的几率,这时,一定要保证在某一个时刻,只有一个线程在对文件进行读写操作。那么其他访问文件的线程该怎么办呢?就像去ATM取钱一样,当ATM正在被使用时,那么其他想要使用ATM

文件修改始终是一件很麻烦也很出错的行为。多线程读写文件更是会加大文件内容混乱的几率,这时,一定要保证在某一个时刻,只有一个线程在对文件进行读写操作。那么其他访问文件的线程该怎么办呢?就像去ATM取钱一样,当ATM正在被使用时,那么其他想要使用ATM的人只能等待知道ATM能使用。

读写文件也一样,当一个线程获得文件时,给予这个线程文件锁。只有拥有文件锁的线程才能操作文件,其他线程就需要一直等待,直到获得文件锁。

下面的代码实现了如下的功能:

读取文件内容;

对内容进行修改;

将修改后的内容写入文件;

替换掉原有的内容。

需要注意的地方有:

文件锁定;

替换文件内容时文件指针的位置;

新内容写入文件时,会覆盖掉原有的内容。注意是覆盖。过新的内容比原有内容少,那么只会覆盖掉新内容长度的字符。例如原有内容是:ab,新写入的内容是:1,些动作完成只够,文件的内容是:1b。因此这里要注意选择。

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);
}
}

因为读写都是用的同一个RandomAccessFile,所以当读取动作执行完成之后,此时的文件指针已经在文件内容末尾了。要替换掉原有的内容就需要将指针移到文件首部。需要

write.seek(0);这个方法来实现。此时写入的内容就会覆盖掉原来文件中的内容。

不足:

在获取文件锁的地方:

while(true){
try {
lock = channel.lock();
break;
} catch (Exception e) {
System.out.println("write::: some other thread is holding the file...");
}
}

不论怎么看都觉得有点别扭,你是否有好的解决方案呢?