对RandomAccessFile类的应用

来源:互联网 发布:数值的整数次方 java 编辑:程序博客网 时间:2024/06/03 13:26

这里写图片描述

实现代码如下:
Employee.java代码:

public class Employee {    public String name = null;    public int age = 0;    public static final int LEN = 8;    public Employee(String name, int age) {        if (name.length() > LEN) {            name = name.substring(0, 8);        } else {            while (name.length() < LEN) {                name += "\u0000";            }        }        this.name = name;        this.age = age;    }}

RandomFileTest.java文件代码:

import java.io.*;public class RandomFileTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        Employee e1 = new Employee("zhangsan", 283);        Employee e2 = new Employee("lisi", 24);        Employee e3 = new Employee("wangwu", 25);        try {            RandomAccessFile ra = new RandomAccessFile("Employee.txt", "rw");            ra.write(e1.name.getBytes());            ra.writeInt(e1.age); // 调用writeInt方法可以写4个字节的整数,如果用write就只能写一个字节,那么age的最大值为256            ra.write(e2.name.getBytes());            ra.writeInt(e2.age);            ra.write(e3.name.getBytes());            ra.writeInt(e3.age);            ra.close();            int len = 0;            byte[] buf = new byte[Employee.LEN];            String strname = null;            RandomAccessFile raf = new RandomAccessFile("Employee.txt", "rw");            raf.skipBytes(Employee.LEN + 4); // 读取第二个记录            len = raf.read(buf);            strname = new String(buf, 0, len);            System.out.println(strname.trim() + ":" + raf.readInt()); // trim可去掉字符串后面的空格            raf.seek(0); // 读取第二个记录,seek函数为绝对定位            len = raf.read(buf);            strname = new String(buf, 0, len);            System.out.println(strname.trim() + ":" + raf.readInt()); // 用readInt读int的,用read就是读一个字节            raf.skipBytes(Employee.LEN + 4);            len = raf.read(buf);            strname = new String(buf, 0, len);            System.out.println(strname.trim() + ":" + raf.readInt());            raf.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

运行结果:
这里写图片描述

要想显示中文,代码就要修改,因为getBytes函数使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中,所以中文有两个字节,英文只有一个字节。要它的写入与平台无关就要使用writeChars函数,修改代码如下:
RandomFileTest.java文件里修改:

import java.io.*;public class RandomFileTest {    public static void main(String[] args) {        Employee e1 = new Employee("张三", 283);        Employee e2 = new Employee("lisi", 24);        Employee e3 = new Employee("wangwu", 25);        try {            RandomAccessFile ra = new RandomAccessFile("Employee.txt", "rw");            ra.writeChars(e1.name);            ra.writeInt(e1.age); // 调用writeInt方法可以写4个字节的整数,如果用write就只能写一个字节,那么age的最大值为256            ra.writeChars(e2.name);            ra.writeInt(e2.age);            ra.writeChars(e3.name);            ra.writeInt(e3.age);            ra.close();            String strname = "";            RandomAccessFile raf = new RandomAccessFile("Employee.txt", "r");            raf.skipBytes(Employee.LEN * 2 + 4); // 读取第二个记录            for (int i = 0; i < Employee.LEN; i++) {                strname += raf.readChar();            }            System.out.println(strname.trim() + ":" + raf.readInt()); // trim可去掉字符串后面的空格            raf.seek(0); // 读取第二个记录,seek函数为绝对定位            strname = "";            for (int i = 0; i < Employee.LEN; i++) {                strname += raf.readChar();            }            System.out.println(strname.trim() + ":" + raf.readInt()); // 用readInt读int的,用read就是读一个字节            raf.skipBytes(Employee.LEN * 2 + 4);            strname = "";            for (int i = 0; i < Employee.LEN; i++) {                strname += raf.readChar();            }            System.out.println(strname.trim() + ":" + raf.readInt());            raf.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

运行结果:

这里写图片描述

1 0
原创粉丝点击