zoukankan      html  css  js  c++  java
  • Java IO流:(十二)RandomAccessFile

    一、RandomAccessFile 类概述

      1、RandomAccessFile 声明在java.io包下,但直接继承于java.lang.Object类。 并且它实现了DataInput、 DataOutput这两个接口,也就意味着这个类既可以读也可以写。

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

        ① 支持只访问文件的部分内存;

        ② 可以向已存在的文件后追加内容;

      3、RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。

    二、RandomAccessFile 类

      1、RandomAccessFile 类继承结构

        

      2、RandomAccessFile 方法列表

        

      3、构造方法

    public RandomAccessFile(String name, String mode){}
    
    public RandomAccessFile(File file, String mode){}
    

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

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

        如果模式为只读 r。则不会创建文件,而是会去读取一个已经存在的文件,如果读取的文件不存在则会出现异常。 如果模式为rw读写。如果文件不存在则会去创建文件,如果存在则不会创建。
      

      4、常用方法

        RandomAccessFile 类对象可以自由移动记录指针:

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

      

      5、说明

        (1)如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建。如果写出到的文件存在,则会对原有文件内容进行覆盖。(默认情况下,从头覆盖);

        (2)可以通过相关的操作,实现 RandomAccessFile “插入”数据的效果;

        (3)JDK1.6上面写的每次 write 数据时,"TW"模式,数据不会立即写到硬盘中;而 fnwd,数据会被立即写入硬盘。如果写数据过程发生异常,‘rwd模式中已被 wit 的被数据彼保存到项盘,而 "rw" 则全部丢失。

     

    三、案例

      1、使用 RandomAccessFile 进行文件的读写

     1     @Test
     2     public void test1() {
     3 
     4         RandomAccessFile raf1 = null;
     5         RandomAccessFile raf2 = null;
     6         try {
     7             //1.
     8             raf1 = new RandomAccessFile(new File("a.jpg"),"r");
     9             raf2 = new RandomAccessFile(new File("a1.jpg"),"rw");
    10             //2.
    11             byte[] buffer = new byte[1024];
    12             int len;
    13             while((len = raf1.read(buffer)) != -1){
    14                 raf2.write(buffer,0,len);
    15             }
    16         } catch (IOException e) {
    17             e.printStackTrace();
    18         } finally {
    19             //3.
    20             if(raf1 != null){
    21                 try {
    22                     raf1.close();
    23                 } catch (IOException e) {
    24                     e.printStackTrace();
    25                 }
    26 
    27             }
    28             if(raf2 != null){
    29                 try {
    30                     raf2.close();
    31                 } catch (IOException e) {
    32                     e.printStackTrace();
    33                 }
    34 
    35             }
    36         }
    37     }
    38 }

      2、RandomAccessFile 会对文件进行覆盖

     1     @Test
     2     public void test2() throws IOException {
     3 
     4         RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");
     5 
     6         raf1.seek(3);//将指针调到角标为3的位置
     7         raf1.write("xyz".getBytes());//会把前三个字符覆盖为 xyz
     8 
     9         raf1.close();
    10 
    11     }

      3、使用RandomAccessFile实现数据的插入效果

     1     @Test
     2     public void test3() throws IOException {
     3 
     4         RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");
     5 
     6         raf1.seek(3);//将指针调到角标为3的位置
     7         //保存指针3后面的所有数据到StringBuilder中
     8         StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
     9         byte[] buffer = new byte[20];
    10         int len;
    11         while((len = raf1.read(buffer)) != -1){
    12             builder.append(new String(buffer,0,len)) ;
    13         }
    14         //调回指针,写入“xyz”
    15         raf1.seek(3);
    16         raf1.write("xyz".getBytes());
    17 
    18         //将StringBuilder中的数据写入到文件中
    19         raf1.write(builder.toString().getBytes());
    20 
    21         raf1.close();
    22 
    23     }

        方式二:使用 ByteArrayOutputStream 实现插入

     1     @Test
     2     public void test3() throws IOException {
     3 
     4         RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");
     5 
     6         raf1.seek(3);//将指针调到角标为3的位置
     7         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     8         byte[] buffer = new byte[1024];
     9         int len;
    10         while ((len = raf1.read(buffer)) != -1) {
    11             baos.write(buffer, 0, len);
    12         }
    13 
    14         raf1.write("xyz".getBytes());
    15         raf1.write(baos.toString().getBytes());
    16         baos.close();
    17         raf1.close();
    18     }

      4、ByteArrayOutputStream 的使用

     1     @Test
     2     public void test1() throws Exception {
     3         FileInputStream fis = new FileInputStream("abc.txt");
     4         String info = readStringFromInputStream(fis);
     5         System.out.println(info);
     6     }
     7 
     8     private String readStringFromInputStream(FileInputStream fis) throws IOException {
     9         // 方式一:可能出现乱码
    10         // String content = "";
    11         // byte[] buffer = new byte[1024];
    12         // int len;
    13         // while((len = fis.read(buffer)) != -1){
    14         // content += new String(buffer);
    15         // }
    16         // return content;
    17 
    18         // 方式二:BufferedReader
    19         BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
    20         char[] buf = new char[10];
    21         int len;
    22         String str = "";
    23         while ((len = reader.read(buf)) != -1) {
    24             str += new String(buf, 0, len);
    25         }
    26         return str;
    27 
    28         // 方式三:避免出现乱码
    29         // ByteArrayOutputStream baos = new ByteArrayOutputStream();
    30         // byte[] buffer = new byte[10];
    31         // int len;
    32         // while ((len = fis.read(buffer)) != -1) {
    33         // baos.write(buffer, 0, len);
    34         // }
    35         //
    36         // return baos.toString();
    37     } 

      5、

    四、

  • 相关阅读:
    Oracle基础操作
    Linux的常用命令
    Javascript 上课笔记
    Linux操作指令
    css样式分类
    DW
    标准sql执行顺序
    Mysql数据库
    模拟课----需求文本
    php 发送邮件
  • 原文地址:https://www.cnblogs.com/niujifei/p/14877850.html
Copyright © 2011-2022 走看看