1. RandomAccessFile類簡介
前面一篇隨筆《File類遍歷目錄及文件》中有說到,File類只能用于表示文件或目錄的名稱、大小等信息,而不能用于文件內容的訪問。而當需要訪問文件內容時,就可以用RandomAccessFile類了。
RandomAccessFile是Java提供用來訪問一些保存數據記錄的文件的類,可以進行讀取操作,也可以進行寫入操作,寫入的數據則以byte的形式存儲;支持隨機訪問,也就是可以訪問文件的任意位置(通過文件指針實現)。
2. 構造函數
1
2
|
RandomAccessFile(String name, String mode) RandomAccessFile(File file, String mode) |
兩個構造函數用法非常相似,name、file都是用于指定打開的文件路徑和名稱,mode則是指定打開文件的方式,常用的參數有兩個"r"和"rw",也就是只讀和讀寫。
文件打開后,文件指針指向文件最開始,也就是pointer=0,可通過RandomAccessFile了的getFilePointer()方法查看。
范例: 創建并打開一個數據文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//創建目錄 File dir = new File( "demo" ); if (!dir.exists()) { dir.mkdir(); } //創建文件 File file = new File(dir, "test.dat" ); if (!file.exists()) { file.createNewFile(); } //實例化RandomAccessFile對象 RandomAccessFile raf = new RandomAccessFile(file, "rw" ); //打開文件時指針位置在最前,即0 System.out.println(raf.getFilePointer()); |
3. 寫入操作
1
2
3
|
write( int i) write( byte [] b) write( byte [] b, int off, int len) |
第三個方法中的off為數組b中需要寫入的數據的起始索引值,len則是要寫入的長度。write方法每次寫入一個字節,如果寫入的數據超過一個字節,則寫入后八位(如果這里不太理解,可看看:二進制運算基礎)。
另外,每寫入一個字節,文件指針指向下一個字節。
范例: 通過write()方法向文件中寫入一個整型數。(沿用上面例子創建的對象)
1
2
3
4
5
6
|
//write()方法每次只插入一個字節,大于一個字節的則寫入后八位,因此寫入一個整型數需要寫入四次 int num = 28 ; raf.write(num >>> 24 ); raf.write(num >>> 16 ); raf.write(num >>> 8 ); raf.write(num); |
當然,RandomAccessFile類也提供了更簡便的方法writeXxx(),如果插入一個整型,可直接writeInt(i);,boolean的則為writeBoolean(),以此類推。但是要清楚的是,這些方法的還是通過上面的write()方法實現的。
范例: 以下為RandomAccessFile類中writeInt()方法的方法體。
1
2
3
4
5
6
7
|
public final void writeInt( int v) throws IOException { write((v >>> 24 ) & xFF); write((v >>> 16 ) & xFF); write((v >>> 8 ) & xFF); write((v >>> 0 ) & xFF); //written += 4; } |
4. 讀取操作
1
2
3
|
read( int i) read( byte [] b) read( byte [] b, int off, int len) |
與寫入操作類似,讀取操作是通過read()方法實現的,每次讀取一個字節,同時文件指針指向下一個位置(通過seek()方法將指針移到讀取位置)。同時,RandomAccessFile類也封裝了readXxx()系列方法用于簡便讀取,原理和使用方法可參考寫入操作,基本類似。
范例: 將數據文件中的所有數據以整型形式讀取出來。
1
2
3
4
5
|
//讀取文件,在讀取前需要通過seek()方法把文件指針移到最前 raf.seek( 0 ); for ( int i = 0 ; i* 4 < raf.length(); i++) { System.out.println(raf.readInt()); } |
5. 關閉文件
打開的文件一定要通過close()關閉,否則可能會出現不可預料的錯誤。
6. 完整例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class MyRandomAccessFile { public static void main(String[] args) throws IOException { //創建目錄 File dir = new File( "demo" ); if (!dir.exists()) { dir.mkdir(); } //創建文件 File file = new File(dir, "test.dat" ); if (!file.exists()) { file.createNewFile(); } //實例化RandomAccessFile對象 RandomAccessFile raf = new RandomAccessFile(file, "rw" ); //打開文件時指針位置在最前,即0 System.out.println(raf.getFilePointer()); //寫入數據 int [] num = { 28 , 14 , 56 , 23 , 98 }; for ( int i : num) { raf.writeInt(i); } //讀取文件,在讀取前需要通過seek()方法把文件指針移到最前 raf.seek( 0 ); for ( int i = 0 ; i* 4 < raf.length(); i++) { System.out.println(raf.readInt()); } //操作結束后一定要關閉文件 raf.close(); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/joahyau/p/6722708.html?utm_source=tuicool&utm_medium=referral