IO_RandomAccessFile

RandomAccessFile

1、RandomAccessFile类支持“随机访问”的方式,程序可以直接跳到文件的任何地方来读、写文件。

  • 支持只访问文件的部分类容
  • 可以向已经存在的内容后追加内容
  • 2、RandomAccessFile对象包含一个记录指针,用以标识当前读写处的位置。RandomAccessFile类对象可以自由移动记录指针:

  • long getFilePointer():获取文件记录指针的当前位置
  • void seek(long pos):将文件记录指针定位到pos位置

3、构造器

  • public RandomAccessFile(File file,String mode);
  • public RandomAccessFile(String name,String mode);

4、创建RandomAccessFile类实例需要一个mode参数,该参数指定RandomAccessFile的访问模式:

  • r:以只读方式打开
  • rw:打开以便读取和写入
  • rwd:打开以便读取和写入;同步文件内容的更新
  • rws:打开以便读取和写入;同步文件内容和元数据的更新

/**
 * @author chenpeng
 * @date 2018/7/9 9:13
 * RandomAccessFileTest:支持随机访问
 * 1、既可以充当输入流,也可以充当输出流
 * 2、支持从文件的开头和读取和写入
 * 3、支持从任意位置读取、写入(插入)
 */
public class RandomAccessFileTest {
    /**
     * 进行文件的读写
     */
    @Test
    public void test1(){
        RandomAccessFile raf1 = null;
        RandomAccessFile raf2 = null;
        try {
            raf1 = new RandomAccessFile(new File("F:/test/2.txt"),"r");
            raf2 = new RandomAccessFile(new File("F:/test/1.txt"),"rw");

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = raf1.read(bytes))!=-1){
                raf2.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (raf2!=null){
                    raf2.close();
                }
                if (raf1!=null){
                    raf1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 在任意位置写入,实际上实现的是覆盖的效果
     */
    @Test
    public void test2(){
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(new File("F:/test/1.txt"),"rw");
            raf.seek(3);
            raf.write("lalalalala".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf!=null){
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 实现插入的效果
     */
    @Test
    public void test3(){
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(new File("F:/test/1.txt"),"rw");

           /* raf.seek(3);
            //先读出
            String str = raf.readLine();

            raf.seek(3);
            raf.write("ccc".getBytes());
            raf.write(str.getBytes());
*/
            //优化版
            raf.seek(3);
            byte[] bytes = new byte[1024];
            int len ;
            StringBuffer sb = new StringBuffer();
            while ((len = raf.read(bytes))!=-1){
                sb.append(new String(bytes,0,len));
            }
            raf.seek(3);
            raf.write("aaa".getBytes());
            raf.write(sb.toString().getBytes());


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf!=null){
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值