1.随机存取文件流:RandomAccessFile
使用说明:
1.RandomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口
2.RandomAccessFile既可以作为一个输入流,又可以作为一个输出流
3.如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建。如果写出到的文件存在,则会对原文件内容进行覆盖。(默认情况下,从头覆盖)
4.可以通过相关的操作,实现RandomAccessFile“插入”数据的效果。seek(int pos)
2.代码演示
2.1 典型代码1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| @Test public void test1() {
RandomAccessFile raf1 = null; RandomAccessFile raf2 = null; try { raf1 = new RandomAccessFile(new File("爱情与友情.jpg"),"r"); raf2 = new RandomAccessFile(new File("爱情与友情1.jpg"),"rw"); byte[] buffer = new byte[1024]; int len; while((len = raf1.read(buffer)) != -1){ raf2.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { if(raf1 != null){ try { raf1.close(); } catch (IOException e) { e.printStackTrace(); }
} if(raf2 != null){ try { raf2.close(); } catch (IOException e) { e.printStackTrace(); }
} } }
|
2.2 典型代码2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
@Test public void test3() throws IOException {
RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");
raf1.seek(3); StringBuilder builder = new StringBuilder((int) new File("hello.txt").length()); byte[] buffer = new byte[20]; int len; while((len = raf1.read(buffer)) != -1){ builder.append(new String(buffer,0,len)) ; } raf1.seek(3); raf1.write("xyz".getBytes());
raf1.write(builder.toString().getBytes());
raf1.close();
}
|