檔案存取通常是「循序的」,每在檔案中存取一次,讀取檔案的位置就會相對於目前的位置前進,然而有時候您必須對檔案的某個區段進行讀取或寫入的動作,也就是進行「隨機存取」(Random access),也就是說存取檔案的位置要能在檔案中隨意的移動,這時您可以使用RandomAccessFile,使用seek()方法來指定檔案存取的位置,指定的單位是位元組,藉由它您就可以對檔案進行隨機存取的動作。
為了方便,通常在隨機存取檔案時會固定每組資料的長度,例如一組學生個人資料,Java中並沒有像C/C++中可以直接寫入一個固定長度結構(Structure)的方法,所以在固定每組長度的方面您必須自行設計。
下面這個程式示範了如何使用RandomAccessFile來寫入檔案,並隨機讀出一筆您所想讀出的資料:
package onlyfun.caterpillar; public class Student { private String name; // 固定 15 字元 private int score; public Student() { setName("noname"); } public Student(String name, int score) { setName(name); this.score = score; } public void setName(String name) { StringBuilder builder = null; if(name != null) builder = new StringBuilder(name); else builder = new StringBuilder(15); builder.setLength(15); this.name = builder.toString(); } public void setScore(int score) { this.score = score; } public String getName() { return name; } public int getScore() { return score; } // 每筆資料固定寫入34位元組 public static int size() { return 34; } }
- RandomAccessFileDemo.java
package onlyfun.caterpillar; import java.io.*; import java.util.*; public class RandomAccessFileDemo { public static void main(String[] args) { Student[] students = { new Student("Justin", 90), new Student("momor", 95), new Student("Bush", 88), new Student("caterpillar", 84)}; try { File file = new File(args[0]); // 建立RandomAccessFile實例並以讀寫模式開啟檔案 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); for(int i = 0; i < students.length; i++) { randomAccessFile.writeChars(students[i].getName()); randomAccessFile.writeInt(students[i].getScore()); } Scanner scanner = new Scanner(System.in); System.out.print("讀取第幾筆資料?"); int num = scanner.nextInt();
randomAccessFile.seek((num-1) * Student.size()); Student student = new Student();
student.setName(readName(randomAccessFile)); student.setScore(randomAccessFile.readInt());
System.out.println("姓名:" + student.getName()); System.out.println("分數:" + student.getScore()); randomAccessFile.close(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("請指定檔案名稱"); } catch(IOException e) { e.printStackTrace(); } } private static String readName( RandomAccessFile randomAccessfile) throws IOException { char[] name = new char[15]; for(int i = 0; i < name.length; i++) name[i] = randomAccessfile.readChar(); return new String(name).replace('\0', ' '); } }
在實例化一個RandomAccessFile物件時,要設定檔案開啟的方式,設定"r"表示只供讀取,設定"rw"表示可讀可寫;為了讓每組資料長度固
定,在寫入name時,我們使用 StringBuilder
並設定其長度固定為15個字元,而讀回name時則直接讀回15個字元,然後再去掉空白字元傳回。 |
|