文件修改始终是一件很麻烦也很出错的行为。多线程读写文件更是会加大文件内容混乱的概率,这时,必定要保证在某一个时刻,只有一个线程在对文件进行读写操做。那么其余访问文件的线程该怎么办呢?就像去ATM取钱同样,当ATM正在被使用时,那么其余想要使用ATM的人只能等待知道ATM能使用。java
读写文件也同样,当一个线程得到文件时,给予这个线程文件锁。只有拥有文件锁的线程才能操做文件,其余线程就须要一直等待,直到得到文件锁。多线程
下面的代码实现了以下的功能:app
读取文件内容;
对内容进行修改;
将修改后的内容写入文件;
替换掉原有的内容。
须要注意的地方有:dom
文件锁定;
替换文件内容时文件指针的位置;
新内容写入文件时,会覆盖掉原有的内容。注意是覆盖。过新的内容比原有内容少,那么只会覆盖掉新内容长度的字符。例如原有内容是: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...");
}
}
不论怎么看都以为有点别扭,你是否有好的解决方案呢?