日期:2014-05-20 浏览次数:20806 次
void insert(String filename,int pos,String insertContent){//pos是插入的位置
File tmp = File.createTempFile("tmp",null);
tmp.deleteOnExit();
try{
RandomAccessFile raf = new RandomAccessFile(filename,"rw");
FileOutputStream tmpOut = new FileOutputStream(tmp);
FileInputStream tmpIn = new FileInputStream(tmp);
raf.seek(pos);//首先的话是0
byte[] buf = new byte[64];
int hasRead = 0;
while((hasRead = raf.read(buf))>0){
//把原有内容读入临时文件
tmpOut.write(buf,0,hasRead);
}
raf.seek(pos);
raf.write(insertContent.getBytes());
//追加临时文件的内容
while((hasRead = tmpIn.read(buf))>0){
raf.write(buf,0,hasRead);
}
}
}