Java RandomAccessFile的使用

Java的RandomAccessFile提供对文件的读写功能,与普通的输入输出流不一样的是RamdomAccessFile可以任意的访问文件的任何地方。这就是“Random”的意义所在。

RandomAccessFile的对象包含一个记录指针,用于标识当前流的读写位置,这个位置可以向前移动,也可以向后移动。RandomAccessFile包含两个方法来操作文件记录指针。

long getFilePoint():记录文件指针的当前位置。

void seek(long pos):将文件记录指针定位到pos位置。

RandomAccessFile包含InputStream的三个read方法,也包含OutputStream的三个write方法。同时RandomAccessFile还包含一系列的readXxx和writeXxx方法完成输入输出。

RandomAccessFile的构造方法如下

 \

mode的值有四个

"r":以只读文方式打开指定文件。如果你写的话会有IOException。

"rw":以读写方式打开指定文件,不存在就创建新文件。

"rws":不介绍了。

"rwd":也不介绍。

[java] view plain copy

  1. /** 

  2.  * 往文件中依次写入3名员工的信息, 

  3.  * 每位员工有姓名和员工两个字段 然后按照 

  4.  * 第二名,第一名,第三名的先后顺序读取员工信息 

  5.  */  

  6. import java.io.File;  

  7. import java.io.RandomAccessFile;  

  8.   

  9. public class RandomAccessFileTest {  

  10.     public static void main(String[] args) throws Exception {  

  11.         Employee e1 = new Employee(23"张三");  

  12.         Employee e2 = new Employee(24"lisi");  

  13.         Employee e3 = new Employee(25"王五");  

  14.         File file = new File("employee.txt");  

  15.         if (!file.exists()) {  

  16.             file.createNewFile();  

  17.         }  

  18.         // 一个中文占两个字节 一个英文字母占一个字节  

  19.         // 整形 占的字节数目 跟cpu位长有关 32位的占4个字节  

  20.         RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");  

  21.         randomAccessFile.writeChars(e1.getName());  

  22.         randomAccessFile.writeInt(e1.getAge());  

  23.         randomAccessFile.writeChars(e2.getName());  

  24.         randomAccessFile.writeInt(e2.getAge());  

  25.         randomAccessFile.writeChars(e3.getName());  

  26.         randomAccessFile.writeInt(e3.getAge());  

  27.         randomAccessFile.close();  

  28.   

  29.         RandomAccessFile raf2 = new RandomAccessFile(file, "r");  

  30.         raf2.skipBytes(Employee.LEN * 2 + 4);  

  31.         String strName2 = "";  

  32.         for (int i = 0; i < Employee.LEN; i++) {  

  33.             strName2 = strName2 + raf2.readChar();  

  34.         }  

  35.         int age2 = raf2.readInt();  

  36.         System.out.println("strName2 = " + strName2.trim());  

  37.         System.out.println("age2 = " + age2);  

  38.   

  39.         raf2.seek(0);  

  40.         String strName1 = "";  

  41.         for (int i = 0; i < Employee.LEN; i++) {  

  42.             strName1 = strName1 + raf2.readChar();  

  43.         }  

  44.         int age1 = raf2.readInt();  

  45.         System.out.println("strName1 = " + strName1.trim());  

  46.         System.out.println("age1 = " + age1);  

  47.   

  48.         raf2.skipBytes(Employee.LEN * 2 + 4);  

  49.         String strName3 = "";  

  50.         for (int i = 0; i < Employee.LEN; i++) {  

  51.             strName3 = strName3 + raf2.readChar();  

  52.         }  

  53.         int age3 = raf2.readInt();  

  54.         System.out.println("strName3 = " + strName3.trim());  

  55.         System.out.println("age3 = " + age3);  

  56.     }  

  57. }  

  58.   

  59. class Employee {  

  60.     // 年龄  

  61.     public int age;  

  62.     // 姓名  

  63.     public String name;  

  64.     // 姓名的长度  

  65.     public static final int LEN = 8;  

  66.   

  67.     public Employee(int age, String name) {  

  68.         this.age = age;  

  69.   

  70.         // 对name字符长度的一个处理  

  71.         if (name.length() > LEN) {  

  72.             name = name.substring(0, LEN);  

  73.         } else {  

  74.             while (name.length() < LEN) {  

  75.                 name = name + "/u0000";  

  76.             }  

  77.         }  

  78.         this.name = name;  

  79.     }  

  80.   

  81.     public int getAge() {  

  82.         return age;  

  83.     }  

  84.   

  85.     public String getName() {  

  86.         return name;  

  87.     }  

  88.   

  89. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章