RandomAccessFile 隨機訪問檔案類

2021-09-13 22:21:43 字數 3502 閱讀 6193

randomaccessfile

public

class

randomaccessfile

implements

dataoutput

, datainput, closeable

randomaccessfile 類繼承於object,並實現介面datainput和dataoutput中定義的讀取/輸出基本型別值和字串的方法,並實現closeable可關閉的介面;

//根據檔名構造

public

randomaccessfile

(string name, string mode)

throws filenotfoundexception

//根據檔案構造

public

randomaccessfile

(file file, string mode)

throws filenotfoundexception

}if(imode <0)

throw

newillegalargumentexception

("illegal mode \""

+ mode

+"\" must be one of "

+"\"r\", \"rw\", \"rws\","

+" or \"rwd\"");

securitymanager security = system.

getsecuritymanager()

;if(security != null)}if

(name == null)

if(file.

isinvalid()

) fd =

newfiledescriptor()

; fd.

incrementandgetusecount()

;this

.path = name;

open

(name, imode)

;}

randomaccessfile(string name, string mode)

randomaccessfile(file file, string mode)

其中,mode 是指定開啟檔案的訪問模式,有以下4中:

(1)「r」:當前檔案支援唯讀操作。任何寫入操作將導致丟擲ioexception。

(2)「rw」:當前檔案支援讀寫操作。如果該檔案不存在,則嘗試建立該檔案。

(3)「rws」:支援讀寫操作,並且將檔案內容和元資料同步到儲存介質。

(4)「rwd」:支援讀寫操作,並且將檔案內容同步到底層儲存介質。

native void open(string name, int mode):開啟檔案操作,以特定方式開啟

filechannel getchannel(): 返回與此檔案關聯的唯一 filechannel 物件。

int read(): 從此檔案中讀取乙個資料位元組。

int read(byte b, int off, int len): 將最多 len 個資料位元組從此檔案讀入 byte 陣列。

int read(byte b):將最多 b.length 個資料位元組從此檔案讀入 byte 陣列。

void readfully(byte b): 將 b.length 個位元組從此檔案讀入 byte 陣列,並從當前檔案指標開始。

void readfully(byte b, int off, int len) :將正好 len 個位元組從此檔案讀入 byte 陣列,並從當前檔案指標開始。

int skipbytes(int n):嘗試跳過輸入的 n 個位元組以丟棄跳過的位元組。

void write(int b): 向此檔案寫入指定的位元組。

void writebytes(byte b, int off, int len):按位元組序列將該字串寫入該檔案。

void write(byte b): 將 b.length 個位元組從指定 byte 陣列寫入到此檔案,並從當前檔案指標開始。

void write(byte b, int off, int len):將 len 個位元組從指定 byte 陣列寫入到此檔案,並從偏移量 off 處開始。

native long getfilepointer():返回此檔案中的當前偏移量。

native void setlength(long newlength): 設定此檔案的長度。

void close()

boolean readboolean(): 從此檔案讀取乙個 boolean。

byte readbyte(): 從此檔案讀取乙個有符號的八位值。

char readchar():從此檔案讀取乙個字元。

double readdouble():從此檔案讀取乙個 double。

float readfloat():從此檔案讀取乙個 float。

int readint():從此檔案讀取乙個有符號的 32 位整數。

void writeboolean(boolean v) :按單位元組值將 boolean 寫入該檔案。

void writebyte(int v):按單位元組值將 byte 寫入該檔案。

void writebytes(string s): 按位元組序列將該字串寫入該檔案。

void writechar(int v): 按雙位元組值將 char 寫入該檔案,先寫高位元組。

void writechars(string s): 按字串行將乙個字串寫入該檔案。

void writedouble(double v): 使用 double 類中的 doubletolongbits 方法將雙精度引數轉換為乙個 long,然後按八字節數量將該 long 值寫入該檔案,先定高位元組。

void writefloat(float v):使用 float 類中的 floattointbits 方法將浮點引數轉換為乙個 int,然後按四位元組數量將該 int 值寫入該檔案,先寫高位元組。

void writeint(int v):按四個位元組將 int 寫入該檔案,先寫高位元組。

void writelong(long v):按八個位元組將 long 寫入該檔案,先寫高位元組。

void writeshort(int v):按兩個位元組將 short 寫入該檔案,先寫高位元組。

void writeutf(string str):使用 modified utf-8 編碼以與機器無關的方式將乙個字串寫入該檔案。

特有方法:

native

long

getfilepointer

():返回此檔案中的當前偏移量。

native

void

seek

(long pos): 設定到此檔案開頭測量到的檔案指標偏移量,在該位置發生下乙個讀取或寫入操作。

native

long

length

():返回此檔案的長度。

RandomAccessFile隨機訪問分割檔案

分割檔案 author administrator public class spiltfile public spiltfile string filepath 呼叫這個構造 public spiltfile string filepath,long blocksize public void i...

RandomAccessFile 檔案斷點拷貝

檔案正常拷貝過程中,由於客觀原因出現異常,導致拷貝中途停止 下一次拷貝時讀取目標檔案中上次拷貝的終止位置,將檔案指標設定到該位置,繼續拷貝。file file newfile c users k u n desktop 35.mp4 file target newfile io lesson fil...

RandomAccessFile 對檔案隨機訪問

檔案是什麼 檔案中儲存的是資料 任何檔案都是有byte資料組成的有序的數列 byte資料 是檔案的最小 原子 不可在分 單位 1 new randomaccessfile file file,string mode 2 new randomaccessfile string filename,str...