7-RandomAccessFile 随机流

  1 package com.io;
  2 
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.IOException;
  6 import java.io.RandomAccessFile;
  7 
  8 import org.junit.Test;
  9 
 10 /*
 11  *  RandomAccessFile:支持随机访问
 12  *    1、即可充当一个输入流,又可充当一个输出流
 13  *    2、支持从任意位置读写
 14  */
 15 public class RandomAccessFileTest {
 16     
 17     
 18 
 19     @Test
 20     public void testRandomAccessFile(){
 21         /*
 22          * 创建RandomAccessFile对象有两个参数,
 23          * 一个是file对象,一个是此RandomAccessFile对象对文件读写的权限,有以下四种 
 24          * 
 25          * r: 以只读方式打开
 26          * rw:打开以便读取和写入
 27          * rwd:打开以便读取和写入;同步文件内容的更新
 28          * rws:打开以便读取和写入;同步文件内容和元数据的更新
 29 
 30          */
 31         RandomAccessFile raf_read = null;
 32         RandomAccessFile raf_write = null;
 33         try {
 34             raf_read = new RandomAccessFile(new File("random.txt"), "r");
 35             raf_write = new RandomAccessFile(new File("random_new.txt"), "rw");
 36 
 37             byte[] b = new byte[20];
 38             int len;
 39             while((len = raf_read.read(b)) != -1){
 40                 raf_write.write(b, 0, len);
 41             };
 42         } catch (FileNotFoundException e) {
 43             // TODO Auto-generated catch block
 44             e.printStackTrace();
 45         } catch (IOException e) {
 46             // TODO Auto-generated catch block
 47             e.printStackTrace();
 48         }finally{
 49             try {
 50                 raf_write.close();
 51             } catch (IOException e) {
 52                 e.printStackTrace();
 53             }
 54             
 55             try {
 56                 raf_read.close();
 57             } catch (IOException e) {
 58                 e.printStackTrace();
 59             }
 60         }
 61     }
 62     
 63     
 64     @Test
 65     public void 覆盖(){
 66         RandomAccessFile raf = null;
 67         try {
 68             raf = new RandomAccessFile(new File("random.txt"), "rw");
 69             raf.seek(3);//把指针移到第3个字节
 70             raf.write("xy".getBytes());
 71         }  catch (IOException e) {
 72             e.printStackTrace();
 73         }finally{
 74         try {
 75             raf.close();
 76         } catch (IOException e) {
 77             // TODO Auto-generated catch block
 78             e.printStackTrace();
 79         }
 80         }
 81     }
 82     
 83     //单行插入字符
 84     @Test
 85     public void 插入字符串(){
 86         RandomAccessFile raf = null;
 87         try {
 88             raf = new RandomAccessFile(new File("random.txt"), "rw");
 89             raf.seek(3);//把指针移到第3个字节
 90             String str = raf.readLine();
 91             System.out.println(raf.getFilePointer());//指针位置
 92             raf.seek(3);
 93             raf.write("xy".getBytes());
 94             raf.write(str.getBytes());
 95         }  catch (IOException e) {
 96             e.printStackTrace();
 97         }finally{
 98         try {
 99             raf.close();
100         } catch (IOException e) {
101             // TODO Auto-generated catch block
102             e.printStackTrace();
103         }
104         }
105     }
106     
107     
108     
109     //多行插入字符
110         @Test
111         public void 多行插入字符串(){
112             RandomAccessFile raf = null;
113             try {
114                 raf = new RandomAccessFile(new File("random_lines.txt"), "rw");
115                 raf.seek(3);//把指针移到第3个字节
116                  byte [] b = new byte[20];
117                  int len;
118                  StringBuffer sbr = new StringBuffer();
119                  while((len = raf.read(b)) != -1){
120                      sbr.append(new String(b,0,len));
121                  }
122                  System.out.println(raf.getFilePointer());
123                  raf.seek(3);
124                  raf.write("xy".getBytes());
125             }  catch (IOException e) {
126                 e.printStackTrace();
127             }finally{
128             try {
129                 raf.close();
130             } catch (IOException e) {
131                 e.printStackTrace();
132             }
133             }
134         }
135 }

 

posted @ 2015-12-07 15:11  浮白斋主人  阅读(215)  评论(0编辑  收藏  举报