/**
*
* @param fileName 文件
* @param pos 插入位置
* @param insertContent 插入内容
* @throws IOException
*/
public static void insert(String fileName, long pos, String insertContent)throws IOException{
RandomAccessFile raf = null;
File tmp = File.createTempFile("tmp", null);
FileOutputStream tmpOut = null;
FileInputStream tmpIn = null;
tmp.deleteOnExit();
try{
raf = new RandomAccessFile(fileName, "rw");
tmpOut = new FileOutputStream(tmp);
tmpIn = new FileInputStream(tmp);
raf.seek(pos);
byte[] bbuf = new byte[64];
int hasRead = 0;
while ((hasRead = raf.read(bbuf)) > 0){
tmpOut.write(bbuf, 0, hasRead);
}
raf.seek(pos);
raf.write(insertContent.getBytes());
while ((hasRead = tmpIn.read(bbuf)) > 0){
raf.write(bbuf, 0, hasRead);
}
}finally{
raf.close();
}
}
测试:
public static void main(String[] args) throws IOException {
insert("D:/content.txt", 0,"abc");
}