复制代码代码如下:
impor java.io.File;
impor java.io.FileInputStream;
impor java.io.FileOutputStream;
impor java.io.IOException;
impor java.io.RandomAccessFile;
kelas publik Sisipkan Konten {
penyisipan kekosongan statis publik (String nama file, pos panjang, String insertContent) melempar IOException{
File file = File.createTempFile("tmp", null);
file.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(Namafile, "rw");
FileInputStream fileInputStream = FileInputStream baru(file);
FileOutputStream fileOutputStream = FileOutputStream baru(file);
raf.seek(pos);
byte[] buff = byte baru[64];
int telahBaca = 0;
while((hasRead = raf.read(buff)) > 0){
fileOutputStream.write(penggemar);
}
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) melempar IOException {
insert("F:/AtendanceActivity.java", 57, "插入的内容rn");
}
}