複製程式碼如下:
導入java.io.File;
導入 java.io.FileInputStream;
導入 java.io.FileOutputStream;
導入java.io.IOException;
導入 java.io.RandomAccessFile;
公共類別插入內容{
公共靜態無效插入(字串檔名,長pos,字串insertContent)拋出IOException{
文件 file = File.createTempFile("tmp", null);
文件.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(檔案名稱, "rw");
FileInputStream fileInputStream = new FileInputStream(檔案);
FileOutputStream fileOutputStream = new FileOutputStream(文件);
raf.seek(pos);
byte[] buff = 新位元組[64];
int 已讀 = 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();
檔案輸入流.close();
檔案輸出流.close();
}
公共靜態無效主(字串[] args)拋出IOException {
insert("F:/AttendanceActivity.java", 57, "插入的內容rn");
}
}