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();
}
}
}

posted @ 2017-04-19 20:24  负零度  阅读(223)  评论(0编辑  收藏  举报