java中java.io.RandomAccessFile的應用場景及使用例項
應用場景:
1、向10G檔案末尾插入指定內容,或者向指定指標位置進行插入或者修改內容。
2、斷點續傳,使用seek()方法不斷的更新下載資源的位置。
簡介:
Java中的RandomAccessFile提供了對檔案的讀寫功能。RandomAccessFile 雖然屬於java.io下的類,但它不是InputStream或者OutputStream的子類;它也不同於FileInputStream和FileOutputStream。 FileInputStream 只能對檔案進行讀操作,而FileOutputStream 只能對檔案進行寫操作;但是,RandomAccessFile 與輸入流和輸出流不同之處就是RandomAccessFile可以訪問檔案的任意地方同時支援檔案的讀和寫,並且它支援隨機訪問。RandomAccessFile包含InputStream的三個read方法,也包含OutputStream的三個write方法。同時RandomAccessFile還包含一系列的readXxx和writeXxx方法完成輸入輸出。
RandomAccessFile父類:java.lang.Object
所有介面實現:Closeable, DataInput, DataOutput, AutoCloseable
建構函式:
建立隨機訪問檔案流,以從File引數指定的檔案中讀取,並可選擇寫入檔案。
RandomAccessFile(File file, String mode)建立隨機訪問檔案流,以從中指定名稱的檔案讀取,並可選擇寫入檔案。
RandomAccessFile(String name, String mode)
建構函式中mode引數傳值介紹:
- r 代表以只讀方式開啟指定檔案 。
- rw 以讀寫方式開啟指定檔案 。
- rws 讀寫方式開啟,並對內容或元資料都同步寫入底層儲存裝置 。
- rwd 讀寫方式開啟,對檔案內容的更新同步更新至底層儲存裝置 。
RandomAccessFile包含了一個物件記錄的指標,用於標識當前流的讀寫位置RandomAccessFile包含兩個方法來操作檔案記錄指標。檔案指標可以通過getFilePointer方法讀取,並由seek方法設定。
long getFilePoint():設定檔案指標偏移,從該檔案的開頭測量,發生下一次讀取或寫入。(前面是文件原文翻譯通俗一點就是:返回檔案記錄指標的當前位置,不指定指標的位置預設是0。)
void seek(long pos):設定檔案指標偏移,從該檔案的開頭測量,發生下一次讀取或寫入。(前面是文件原文翻譯通俗一點就是:將檔案記錄指標定位到pos位置。)
以下是讀取和寫入檔案的例項:
1、讀指定檔案的內容,並且輸出控制檯:
public static void main(String[] args) throws Exception{
RandomAccessFile raf=new RandomAccessFile("G:\\java-lambda\\work.txt","r");
byte[] buff = new byte[1024];
int len = 0;
while ((len = raf.read(buff,0,1024))!=-1){
System.out.println(new String(buff,0,len));
}
}
2、通過指定記錄指標的位置及跳過的位元組數,輸出內容
public static void RandomAccess(String path,String mode,long seek,int skipBytes){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(path,mode);
raf.writeBytes("If you interesting to me,please give the kiss to me!");
raf.seek(seek);//指定記錄指標的位置
//System.out.println(raf.readLine());//使用seek指標指向0,readLine讀取所有內容
raf.getFilePointer();//獲取指標的位置
raf.skipBytes(skipBytes);//跳過的位元組數
byte[] buff = new byte[1024];
int len = 0;
while ((len = raf.read(buff))>0){
System.out.println(new String(buff,0,len));
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(raf != null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3、在內容後面插入一個字串,並輸出
public static void RandomAccessTem(String path,String mode,long seek,int skipBytes){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(path,mode);
raf.writeBytes("If you interesting to me,please give the kiss to me!");
raf.seek(raf.length());//通過raf.length()獲取總長度,記錄指標指向最後
raf.write("oh yes!".getBytes());//在最後插入oh yes!s
raf.seek(0);//記錄指標指向初始位置
byte[] buff = new byte[1024];
int len = 0;
while ((len = raf.read(buff))>0){
System.out.println(new String(buff,0,len));
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(raf != null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4、通過臨時檔案插入內容
public static void RandomAccessTemp(String path,String mode){
File tempFile = null;
RandomAccessFile raf = null;
FileOutputStream out = null;
FileInputStream input = null;
try {
//建立臨時檔案
tempFile =File.createTempFile("temp","_temp");
//在JVM程序退出的時候刪除檔案,通常用在臨時檔案的刪除
tempFile.deleteOnExit();
//建立一個臨時資料夾來儲存插入點後的資料
out = new FileOutputStream(tempFile);
input=new FileInputStream(tempFile);
//指定檔案路徑和讀寫方式
raf = new RandomAccessFile(path,mode);
//raf.writeBytes("If you interesting to me,please give the kiss to me!");
//記錄指標指向初始位置
raf.seek(0);
byte[] buff = new byte[1024];
int len = 0;
//迴圈讀取插入點後的內容
//讀取檔案,存入位元組陣列buff,返回讀取到的字元數賦值給len,預設每次將buff陣列裝滿
while ((len = raf.read(buff))>0){
// 將讀取的資料寫入臨時檔案中
out.write(buff,0,len);
System.out.println(new String(buff,0,len));
}
//記錄指標指向初始位置
raf.seek(0);
//插入指定內容
raf.writeBytes("my name's Toms!");
//在臨時檔案中插入指定內容
while ((len = input.read(buff))>0){
raf.write(buff,0,len);
}
out.close();
input.close();
}catch (Exception e){
e.printStackTrace();
}finally {
if(raf != null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
方法摘要
void close() :關閉此隨機訪問檔案流並釋放與該流關聯的所有系統資源。
FileChannel getChannel() : 返回與此檔案關聯的唯一 FileChannel 物件。
FileDescriptor getFD() :返回與此流關聯的不透明檔案描述符物件。
long getFilePointer() :返回此檔案中的當前偏移量。
long length() :返回此檔案的長度。
int read() :從此檔案中讀取一個數據位元組。
int read(byte[] b) : 將最多 b.length 個數據位元組從此檔案讀入 byte 陣列。
int read(byte[] b, int off, int len) :將最多 len 個數據位元組從此檔案讀入 byte 陣列。
boolean readBoolean() :從此檔案讀取一個 boolean。
byte readByte() :從此檔案讀取一個有符號的八位值。
char readChar() :從此檔案讀取一個字元。
double readDouble() :從此檔案讀取一個 double。
float readFloat() :從此檔案讀取一個 float。
void readFully(byte[] b) : 將 b.length 個位元組從此檔案讀入 byte 陣列,並從當前檔案指標開始。
void readFully(byte[] b, int off, int len) :將正好 len 個位元組從此檔案讀入 byte 陣列,並從當前檔案指標開始。
int readInt() :從此檔案讀取一個有符號的 32 位整數。
String readLine() :從此檔案讀取文字的下一行。
long readLong() : 從此檔案讀取一個有符號的 64 位整數。
short readShort() :從此檔案讀取一個有符號的 16 位數。
int readUnsignedByte() :從此檔案讀取一個無符號的八位數。
int readUnsignedShort() :從此檔案讀取一個無符號的 16 位數。
String readUTF() :從此檔案讀取一個字串。
void seek(long pos) :設定到此檔案開頭測量到的檔案指標偏移量,在該位置發生下一個讀取或寫入操作。
void setLength(long newLength) :設定此檔案的長度。
int skipBytes(int n) :嘗試跳過輸入的 n 個位元組以丟棄跳過的位元組。
void write(byte[] b) : 將 b.length 個位元組從指定 byte 陣列寫入到此檔案,並從當前檔案指標開始。
void write(byte[] b, int off, int len) :將 len 個位元組從指定 byte 陣列寫入到此檔案,並從偏移量 off 處開始。
void write(int b) :向此檔案寫入指定的位元組。
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 編碼以與機器無關的方式將一個字串寫入該檔案。