zoukankan      html  css  js  c++  java
  • Java IO 4 : RandomAccessFile

    RandomAccessFile:  

      认识:java输入/输出流体系中功能最丰富的文件内容访问类 既可以读取文件内容,也可以向文件传输数据,并且支持“随机访问“的方式,程序可以跳转到任意地方来读写数据

      特点:与OutputStream/Writer不同,RandomAccessFile允许自由定位记录指针,可以不从开始的地方输出,因此RandomAccessFile可以向已经存在的文件后追加内容,如果程序需要向已经存在的文件后追加内容,应该使用RandomAccessFile

      局限:只能读写文件,不能读写其他IO节点

    RandomAccessFile 两个方法操作文件记录指针

      long getFilePointer()  返回文件记录指针的当前位置
      void seek(long pos)  将文件记录指针定位到pos位置

    RandomAccessFile类为用户提供了两种构造方法:两种方法一个意思,一个是File指定,一个是String指定文件名

      RandomAccessFile(File file, String mode)
      RandomAccessFile(String name, String mode)

    mode的四个值如下:

    模    式 作    用
    r 表示以只读方式打开,调用结果对象的任何write方法都将导致抛出IOException
    rw 打开以便读取和写入,如果该文件尚不存在,则尝试创建该文件
    rws 打开以便读取和写入,相对于"rw",还要求对文件内容或元数据的每个更新都同步写入到底层存储设备
    rwd 打开以便读取和写入,相对于"rw",还要求对文件内容的每个更新都同步写入到底层存储设备


      

    public class File03 {
    
        public static void main(String[] args) throws IOException {
            RandomAccessFile raf = new RandomAccessFile("D://demo/a.txt", "r");
            //初始位置为0
            System.out.println("RandomAccessFile文件指针初始位置:" + raf.getFilePointer());
            raf.seek(10);
            byte[] bbuf = new byte[1024];
            int hasRead = 0;
            while((hasRead = raf.read(bbuf)) > 0) {
                System.out.println(new String(bbuf , 0  , hasRead));
            }
        }
    }

    往文件最后追加内容:

         //往文件里面追加内容
            RandomAccessFile raf = new RandomAccessFile("D://demo/a.txt", "rw");
            raf.seek(raf.length());
            raf.write("这是追加的内容".getBytes());

    往文件中间指定的位置添加内容:

      RandomAccessFile不能向文件指定的内容插入内容,如果直接插,要把指定指针后的内容先读入一个临时的地方存起来, 插入需要的内容后, 再去将临时存储的内容读过来。

      /**
         * 使用RandomAccessFile 向文件指定的位置添加内容
         * @throws IOException 
         */
        public static void insert(String fileName , long pos , String insertContent) throws IOException {
            
            File temp = File.createTempFile("temp", null);
            temp.deleteOnExit(); //jvm 退出时删除临时文件
            
            FileOutputStream tempOut = new FileOutputStream(temp);
            FileInputStream tempIn = new FileInputStream(temp);
            
            RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
            System.out.println(raf.length());
            raf.seek(pos);
            //将插入点后的内容读取到临时文件
            byte[] bbuf = new byte[100];
            int hasRead = 0;
            while((hasRead = raf.read(bbuf)) > 0) {
                tempOut.write(bbuf, 0, hasRead);
            }
            //重新回到插入点 , 将需要添加的内容添加到到文件
            raf.seek(pos);
            raf.write(insertContent.getBytes());
            //将临时文件中的内容添加到原文件当中
            while((hasRead = tempIn.read(bbuf)) > 0) {
                raf.write(bbuf, 0, hasRead);
            }
        }
        
        public static void main(String[] args) throws IOException {
            insert("D://demo/a.txt", 10 , "这是追加到文件中间的内容");
        }

     

     

    温故而知新
  • 相关阅读:
    python requests模块
    python 模拟豆瓣登录(豆瓣6.0)
    python 抓取糗事百科糗图
    python bz2模块
    the python challenge闯关记录(9-16)
    python之PIL库(Image模块)
    python之zipfile
    python之pickle模块
    the python challenge闯关记录(0-8)
    KVO简介
  • 原文地址:https://www.cnblogs.com/Uzai/p/9647813.html
Copyright © 2011-2022 走看看