java任意访问类在任意位置插入数据

class FilesDemo2{
public static void main(String[] args){
File ff = new File("D:\\file.txt");
try(RandomAccessFile f = new RandomAccessFile("D:\\Stream.txt","rwd")
){
ff.createNewFile();
ff.deleteOnExit();
RandomAccessFile f2 = new RandomAccessFile(ff,"rwd");
long a = f.getFilePointer();
System.out.println(a);
f.seek(7);
a = f.getFilePointer();
System.out.println(a);
byte[] arr = new byte[100];
int len = -1;
while((len=f.read(arr))!=-1){
String s = new String(arr);
f2.write(s.getBytes());

}
a = f.getFilePointer();
System.out.println(a);
f.seek(7);
f.write("疑是地上霜。".getBytes());
f.seek(7+"疑是地上霜。".length());
while((len = f2.read(arr)) != -1){
f.write(arr, 0, len);
}

}catch(IOException e ){
e.printStackTrace();
}
}
}

原文地址:https://www.cnblogs.com/01aa/p/6735665.html