Java IO2:RandomAccessFile

一、RandomAccessFile

  RandomAccessFile类可以说是Java语言中功能最为丰富的文件访问类,它提供了众多的文件访问方法。RandomAccessFile类支持"随机访问"方式,可以跳转到文件的任意位置处读写数据。要访问一个文件的时候,不想把文件从头读到尾,而是希望像访问一个数据库一样地访问一个文本文件,使用RandomAccessFile类是最佳选择。

  RandonAccessFile类里面有个位置指示器,指向当前读写处的位置,当已经读写了n个字节后,该指示器将指向这n个字节后的下一个字节处。刚打开文件时,文件指示器指向文件的开头处,也可以移动文件指示器的位置,随后的读写将从新的位置开始。

  RandomAccessFile类在文件随机(相对于顺序)读取时有很大的优势,但该类仅限于操作文件,不能访问其他得IO设备,如网络、内存映像等。

二、RandomAccessFile类的构造方法

  RandomAccessFile类为用户提供了两种构造方法:

  1、RandomAccessFile(File file, String mode)

  2、RandomAccessFile(String name, String mode)

  其实第二种构造方法也是new一个File出来再调用第一种构造方法,建议使用第一种构造方法,因为第一篇文章就说了File是IO的基础,有一个File不仅仅可以通过RandomAccessFile对文件进行操作,也可以通过File对象对文件进行操作。至于mode,Java给开发者提供了四种mode:

  注意第二点"rw"模式,对rw模式的解释意味着Java并不强求指定的路径下一定存在某个文件,假如文件不存在,会自动创建。

三、RandomAccessFile类中的方法

  RandomAccessFile中有如下一些常用方法:

方    法 作    用
void close() 重要,关闭此随机访问文件流并释放与该流关联的所有系统资源
FileChannel getChannel() 返回与此文件关联的唯一FileChannel对象,NIO用到
long getFilePointer() 返回此文件中的当前偏移量
long length() 返回此文件的长度
int read() 从此文件中读取一个数据字节
int read(byte[] b) 将最多b.length个数据字节从此文件读入byte数组,返回读入的总字节数,如果由于已经达到文件末尾而不再有数据,则返回-1。在至少一个输入字节可用前,此方法一直阻塞
int read(byte[] b, int off, int len) 将最多len个数据字节从此文件的指定初始偏移量off读入byte数组
boolean readBoolean() 从此文件读取一个boolean,其余readByte()、readChar()、readDouble()等类似
String readLine() 从此文件读取文本的下一行
void seek(long pos) 重要,设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作
int skipBytes(int n) 重要,尝试跳过输入的n个字节以丢弃跳过的字节,返回跳过的字节数
void write(byte[] b) 将b.length个字节从指定byte数组写入到此文件中
void write(byte[] b, int off, int len) 将len个字节从指定byte数组写入到此文件,并从偏移量off处开始
void write(int b) 向此文件写入指定的字节
void writeBoolean(boolean v) 按单字节值将boolean写入该文件,其余writeByte(int v)、writeBytes(String s)、writeChar(int v)等都类似

四、RandomAccessFile实例

  1、先定义一个Employee实体类:

public class Employee{
    private String name;
    private int age;
    private static final int LEN = 8;

    public Employee(String name, int age) {
        //将名字固定在8个字节
        if(name.length() > 8){
            name = name.substring(0,8);
        }else{
            //名字不够8个字节,用‘u0000’来补齐
            while(name.length() < 8){
                name = name + 'u0000';
            }
        }
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

  其中构造函数中将name固定在了8个字节,'u0000'表示是Character中的最小值,为空

/**
     * The constant value of this field is the smallest value of type
     * {@code char}, {@code 'u005Cu0000'}.
     *
     * @since   1.0.2
     */
    public static final char MIN_VALUE = 'u0000';

  age为int类型,所占4个字节,所以name+age所占12个字节。

  补充说明基本数据类型、位、字节之间的关系

  2、举例说明

  通过RandomAccessFile类将employee对象的name和age写进文件

public class Test {
    public static void main(String[] args) throws IOException {
        Employee employee1 = new Employee("zhangsan",10);
        Employee employee2 = new Employee("lisi",11);
        Employee employee3 = new Employee("wangwu",12);
        File file = new File("D:" + File.separator + "employee.txt");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        randomAccessFile.writeBytes(employee1.getName());
        randomAccessFile.writeInt(employee1.getAge());
        randomAccessFile.writeBytes(employee2.getName());
        randomAccessFile.writeInt(employee2.getAge());
        randomAccessFile.writeBytes(employee3.getName());
        randomAccessFile.writeInt(employee3.getAge());
        randomAccessFile.close();
    }
}

  通过RandomAccessFile类随机读取刚刚写入的文件(通过seek方法和skipBytes方法随机读取文件)

public class Test {
    public static void main(String[] args) throws IOException {
        File file = new File("D:" + File.separator + "employee.txt");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        int length = 8;
        String name = "";
        //跳过第一个人的信息,(姓名占8个字节,age(int)占4个字节)
        randomAccessFile.skipBytes(12);
        System.out.println("第二个人的信息:");
        for(int i = 0; i < length; i++){
            name = name + (char)randomAccessFile.readByte();
        }
        System.out.println("姓名是:" + name);
        System.out.println("年龄是:" + randomAccessFile.readInt());
        //重新定位到文件的开头
        randomAccessFile.seek(0);
        System.out.println("第一个人的信息:");
        name = "";
        for(int i = 0; i < length; i++){
            name = name + (char)randomAccessFile.readByte();
        }
        System.out.println("姓名是:" + name);
        System.out.println("年龄是:" + randomAccessFile.readInt());
        //此时,randomAccessFile的位置指示器已指向第一个人的信息结束的字节码处
        //跳过第二个人的信息(12个字节码 == 姓名的字节码 + int类型的age所占的4个字节码)
        randomAccessFile.skipBytes(12);
        System.out.println("第三个人的信息:");
        name = "";
        for(int i = 0; i < length; i++){
            name = name + (char)randomAccessFile.readByte();
        }
        System.out.println("姓名是:" + name);
        System.out.println("年龄是:" + randomAccessFile.readInt());
        randomAccessFile.close();
    }
}

  结果:

第二个人的信息:
姓名是:lisi    
年龄是:11
第一个人的信息:
姓名是:zhangsan
年龄是:10
第三个人的信息:
姓名是:wangwu  
年龄是:12

  最后,RandomAccessFile使用完一定要及时close()。

参考资料:https://www.cnblogs.com/xrq730/p/4888288.html

原文地址:https://www.cnblogs.com/zfyang2429/p/10472644.html