Java高级编程基础——IO流——处理流——随机存取文件流

一、RandomAccessFile类

  • 直接继承于java.lang.Object类,实现了DataInput和DataOutput接口
  • 既可以作为输入流,也可以作为输出流
  • 作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建文件
    如果写出的文件存在,则会对原有文件内容进行从头覆盖(默认position指针位置为0,调用seek(int pos)方法改变指针位置,则可以实现从指定位置开始覆盖)

构造器

  • RandomAccessFile(String pathname,String mode)
    RandomAccessFile(File filename,String mode)
  • 参数 mode:该参数指定RandomAccesFile的访问模式
    r:以只读方式打开
    rw:打开以便读取和写入
    rwd:打开以便读取和写入;同步文件内容的更新
    rws:打开以便读取和写入;同步文件内容的更新和元数据的更新

实现字节型文件的复制

    @Test
    public void testForRandomAccessFile(){
        RandomAccessFile rafot = null;
        RandomAccessFile rafin = null;

        try {
            //造流
            //RandomAccessFile(String pathname,String mode)
            rafot = new RandomAccessFile(new File("皮卡丘.jpg"),"r");
            rafin = new RandomAccessFile(new File("皮卡丘副本.jpg"),"rw");

            //操作
            byte[] buffer = new byte[1024];
            int len;
            while ((len = rafot.read(buffer)) != -1){
                rafin.write(buffer,0,len);
            }

            System.out.println(rafin.read());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

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



    }

使用RandomAccessFile实现指定位置插入效果

    @Test
    public void test1()  {


        RandomAccessFile  raf1 = null;
        try {
            raf1 = new RandomAccessFile(new File("hello.txt"), "rw");

            raf1.seek(3);//将指针调到第4个字符

            //保存指针3后面的所有数据到StringBuider中
            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));
            }

            //调用指针写入“xyz”
            raf1.seek(3);//将指针调到第4个字符
            raf1.write("xxxxxxx".getBytes());

            //写入StringBuffer中存储的剩余字符串
            raf1.write(builder.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(raf1 != null){
                try {
                    raf1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }

        //关闭流



    }

运行前效果:

在这里插入图片描述

运行后效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值