RandomAccessFile:
RandomAccessFile是Java提供用來訪問一些保存數(shù)據(jù)記錄的文件的類,可以進行讀取操作,也可以進行寫入操作,寫入的數(shù)據(jù)則以byte的形式存儲;支持隨機訪問,也就是可以訪問文件的任意位置(通過文件指針實現(xiàn))。
構造方法:
RandomAccessFile(String name, String mode)
RandomAccessFile(File file, String mode)
其中參數(shù)mode的值可選"r":可讀,"w":可寫,"rw":可讀性;
成員方法:
seek(int index); 可以將指針移動到某個位置開始讀寫;
setLength(long len); 給寫入文件預留空間;
getFilePointer(); 獲取當前位置
寫入操作:
write(int i) //不同類型 可以writeInt()、writeLong() 、writeDouble()
write(byte[] b)
write(byte[] b, int off, int len) //off為數(shù)組b中需要寫入的數(shù)據(jù)的起始索引值,len則是要寫入的長度
讀取操作:
read(int i) //不同類型 可以readInt()、readLong() 、readDouble()
read(byte[] b)
read(byte[] b, int off, int len) //off為數(shù)組b中需讀取的數(shù)據(jù)的起始索引值,len則是要寫入的長度