复制代码代码如下 :
importer java.io.File ;
importer java.io.FileInputStream ;
importer java.io.FileOutputStream ;
importer java.io.IOException ;
importer java.io.RandomAccessFile ;
classe publique InsertContent {
public static void insert (String fileName, long pos, String insertContent) lance IOException {
Fichier fichier = File.createTempFile("tmp", null);
fichier.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
FileInputStream fileInputStream = new FileInputStream(fichier);
FileOutputStream fileOutputStream = new FileOutputStream(fichier);
raf.seek(pos);
octet[] buff = nouvel octet[64];
int hasRead = 0;
while((hasRead = raf.read(buff)) > 0){
fileOutputStream.write(buff);
}
raf.seek(pos);
raf.write(insertContent.getBytes());
//追加文件插入点之后的内容
while((hasRead = fileInputStream.read(buff)) > 0){
raf.write(buff, 0, hasRead);
}
raf.close();
fileInputStream.close();
fileOutputStream.close();
}
public static void main (String[] args) lance IOException {
insert("F:/AttendanceActivity.java", 57, "插入的内容rn");
}
}