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. }  

 

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