java自學之旅(3)測試RandomAccessFile常用方法

java提供了一個可以對文件隨機訪問的操作,訪問包括讀(r)和寫(rw)操作。該類名爲RandomAccessFile。


該類的讀寫是基於指針的操作。


常用方法:


void write(int d)

該方法會根據當前指針所在位置處寫一個字節,是將參數int的低8位寫出。


int read()

該方法會從文件中讀取一個byte(8位)填充到int的低8位,高24位位0,返回值範圍正數:0~255,返回-1表示讀到文件末尾。每次讀取後自動移動文件指針,準備下次讀取。


void write(byte[] d)

根據當前指針所在位置處連續寫出給定數組中的所有字節。


void write(byte[] dt,int offSet, int len)

根據當前指針所在位置處連續寫出給定數組中的部分字節。從數組中offSet開始了連續len個字節。


int read(byte[] d)

從指針位置處嘗試最多讀取給定數組的總長度的字節量,並從給定字節數組第一個位置開始,將讀取到的字節順序存放至數組中,返回值爲實際讀取到的字節量。


void close()

RandomAccessFile在對文件訪問的操作全部結束後,要調用close方法來釋放與其關聯的所有系統資源。


文件指針操作方法:


long getFilePointer

該方法用於獲取當前RandomAccessFile的指針位置。


void seek(long pos)

用於移動當前RandomAccessFile的指針位置。


int skipBytes(int n)

嘗試跳過輸入的n個字節以丟棄跳過的字節。

該方法可能跳過一些較少數量的字節(可能包括0)。這可能由任意數量條件引起;在跳過n個字節前,已到達文件的末尾是一種可能。

此方法不拋出EOFException。返回跳過的實際字節數。如果n爲負數則不跳過任何字節。

package myfile;

import java.io.RandomAccessFile;

/**
 * 測試寫出方法
 * @author Whh
 *
 */
public class TestRandomAccessFile {
	public static void main(String[] args) throws Exception {
		testWrite();
		testRead();
		System.out.println("***********************************");
		testWriteByteArray();
		testReadByteArray();
		System.out.println("***********************************");
		testPointer();
	}
	
	/**測試寫出方法**/
	public static void testWrite() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("raf.text", "rw");
		//寫出一個字節,寫出的是int值得低8位
		System.out.println("指針位置:"+raf.getFilePointer());
		raf.write(2);
		raf.close();
	}
	
	/**測試讀取方法**/
	public static void testRead() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("raf.text", "r");
		int d = raf.read();
		System.out.println(d);
		System.out.println("指針位置:"+raf.getFilePointer());
		raf.close();
	}
	
	/**測試批量寫出**/
	public static void testWriteByteArray() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("raf.text", "rw");
		//將字符串按默認編碼轉化爲字節
		System.out.println("指針位置:"+raf.getFilePointer());
		byte[] buf = "Hello world".getBytes();
		//將字節數組中所有字節一次性寫出
		raf.write(buf);
		raf.close();
	}
	
	/**測試批量讀取**/
	public static void testReadByteArray() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("raf.text", "r");
		//創建長度爲11的字節數組
		byte[] buf = new byte [11];
		//嘗試讀取十個字節存入數組,返回值爲讀取的字節量
		int len = raf.read(buf);
		System.out.println("從當前指針開始讀取到了:"+len+"個字節");
		System.out.println(new String(buf));
		System.out.println("指針位置:"+raf.getFilePointer());
		raf.close();
	}
	
	/**測試指針相關方法**/
	public static void testPointer() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("raf.text", "r");
		//輸出指針位置,默認從零開始(文件的第一個字節位置)
		System.out.println("指針位置:"+raf.getFilePointer());
		//將指針動到位置6,讀取world5個字節
		raf.seek(6);//將指針位置設爲6,下一次操作指針將從位置7開始
		System.out.println("指針位置:"+raf.getFilePointer());
		byte [] buf = new byte[5];
		raf.read(buf);
		System.out.println(new String(buf));
		System.out.println("指針位置:"+raf.getFilePointer());
		raf.close();
	}
}


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