java檔案流讀寫操作
優化BufferedRandomAccessFile。
優化原則:
- 呼叫頻繁的語句最需要優化,且優化的效果最明顯。
- 多重巢狀邏輯判斷時,最可能出現的判斷,應放在最外層。
- 減少不必要的NEW。
這裡舉一典型的例子:
- publicvoid seek(long pos) throws IOException {
- ...
- this.bufstartpos = pos * bufbitlen / bufbitlen; // bufbitlen指buf[]的位長,例:若bufsize=1024,則bufbitlen=10。
- ...
- }
seek函式使用在各函式中,呼叫非常頻繁,上面加重的這行語句根據pos和bufsize確定buf[]對應當前檔案的對映位置,用"*"、"/"確定,顯然不是一個好方法。
- 優化一:this.bufstartpos = (pos << bufbitlen) >> bufbitlen;
- 優化二:this.bufstartpos = pos & bufmask; // this.bufmask = ~((long)this.bufsize - 1);
兩者效率都比原來好,但後者顯然更好,因為前者需要兩次移位運算、後者只需一次邏輯與運算(bufmask可以預先得出)。
至此優化基本實現,逐位元組COPY一個12兆的檔案,(這裡牽涉到讀和寫,結合緩衝讀,用優化後BufferedRandomAccessFile試一下讀/寫的速度):
讀 | 寫 | 耗用時間(秒) |
RandomAccessFile | RandomAccessFile | 95.848 |
BufferedInputStream + DataInputStream | BufferedOutputStream + DataOutputStream | 2.935 |
BufferedRandomAccessFile | BufferedOutputStream + DataOutputStream | 2.813 |
BufferedRandomAccessFile | BufferedRandomAccessFile | 2.453 |
BufferedRandomAccessFile優 | BufferedRandomAccessFile優 | 2.197 |
可見優化儘管不明顯,還是比未優化前快了一些,也許這種效果在老式機上會更明顯。
以上比較的是順序存取,即使是隨機存取,在絕大多數情況下也不止一個BYTE,所以緩衝機制依然有效。而一般的順序存取類要實現隨機存取就不怎麼容易了。
需要完善的地方
提供檔案追加功能:
Java程式碼- publicboolean append(byte bw) throws IOException {
- returnthis.write(bw, this.fileendpos + 1);
- }
提供檔案當前位置修改功能:
Java程式碼- publicboolean write(byte bw) throws IOException {
- returnthis.write(bw, this.curpos);
- }
返回檔案長度(由於BUF讀寫的原因,與原來的RandomAccessFile類有所不同):
Java程式碼- publiclong length() throws IOException {
- returnthis.max(this.fileendpos + 1, this.initfilelen);
- }
返回檔案當前指標(由於是通過BUF讀寫的原因,與原來的RandomAccessFile類有所不同):
Java程式碼- publiclong getFilePointer() throws IOException {
- returnthis.curpos;
- }
提供對當前位置的多個位元組的緩衝寫功能:
Java程式碼- publicvoid write(byte b[], int off, int len) throws IOException {
- long writeendpos = this.curpos + len - 1;
- if (writeendpos <= this.bufendpos) { // b[] in cur buf
- System.arraycopy(b, off, this.buf, (int)(this.curpos - this.bufstartpos), len);
- this.bufdirty = true;
- this.bufusedsize = (int)(writeendpos - this.bufstartpos + 1);
- } else { // b[] not in cur buf
- super.seek(this.curpos);
- super.write(b, off, len);
- }
- if (writeendpos > this.fileendpos)
- this.fileendpos = writeendpos;
- this.seek(writeendpos+1);
- }
- publicvoid write(byte b[]) throws IOException {
- this.write(b, 0, b.length);
- }
提供對當前位置的多個位元組的緩衝讀功能:
Java程式碼- publicint read(byte b[], int off, int len) throws IOException {
- long readendpos = this.curpos + len - 1;
- if (readendpos <= this.bufendpos && readendpos <= this.fileendpos ) { // read in buf
- System.arraycopy(this.buf, (int)(this.curpos - this.bufstartpos), b, off, len);
- } else { // read b[] size > buf[]
- if (readendpos > this.fileendpos) { // read b[] part in file
- len = (int)(this.length() - this.curpos + 1);
- }
- super.seek(this.curpos);
- len = super.read(b, off, len);
- readendpos = this.curpos + len - 1;
- }
- this.seek(readendpos + 1);
- return len;
- }
- publicint read(byte b[]) throws IOException {
- returnthis.read(b, 0, b.length);
- }
- publicvoid setLength(long newLength) throws IOException {
- if (newLength > 0) {
- this.fileendpos = newLength - 1;
- } else {
- this.fileendpos = 0;
- }
- super.setLength(newLength);
- }
- publicvoid close() throws IOException {
- this.flushbuf();
- super.close();
- }
至此完善工作基本完成,試一下新增的多位元組讀/寫功能,通過同時讀/寫1024個位元組,來COPY一個12兆的檔案,(這裡牽涉到讀和寫,用完善後BufferedRandomAccessFile試一下讀/寫的速度):
讀 | 寫 | 耗用時間(秒) |
RandomAccessFile | RandomAccessFile | 95.848 |
BufferedInputStream + DataInputStream | BufferedOutputStream + DataOutputStream | 2.935 |
BufferedRandomAccessFile | BufferedOutputStream + DataOutputStream | 2.813 |
BufferedRandomAccessFile | BufferedRandomAccessFile | 2.453 |
BufferedRandomAccessFile優 | BufferedRandomAccessFile優 | 2.197 |
BufferedRandomAccessFile完 | BufferedRandomAccessFile完 | 0.401 |
與MappedByteBuffer+RandomAccessFile的對比?
JDK1.4+提供了NIO類 ,其中MappedByteBuffer類用於對映緩衝,也可以對映隨機檔案訪問,可見JAVA設計者也看到了RandomAccessFile的問題,並加以改進。怎麼通過MappedByteBuffer+RandomAccessFile拷貝檔案呢?下面就是測試程式的主要部分:
Java程式碼- RandomAccessFile rafi = new RandomAccessFile(SrcFile, "r");
- RandomAccessFile rafo = new RandomAccessFile(DesFile, "rw");
- FileChannel fci = rafi.getChannel();
- FileChannel fco = rafo.getChannel();
- long size = fci.size();
- MappedByteBuffer mbbi = fci.map(FileChannel.MapMode.READ_ONLY, 0, size);
- MappedByteBuffer mbbo = fco.map(FileChannel.MapMode.READ_WRITE, 0, size);
- long start = System.currentTimeMillis();
- for (int i = 0; i < size; i++) {
- byte b = mbbi.get(i);
- mbbo.put(i, b);
- }
- fcin.close();
- fcout.close();
- rafi.close();
- rafo.close();
- System.out.println("Spend: "+(double)(System.currentTimeMillis()-start) / 1000 + "s");
試一下JDK1.4的對映緩衝讀/寫功能,逐位元組COPY一個12兆的檔案,(這裡牽涉到讀和寫):
讀 | 寫 | 耗用時間(秒) |
RandomAccessFile | RandomAccessFile | 95.848 |
BufferedInputStream + DataInputStream | BufferedOutputStream + DataOutputStream | 2.935 |
BufferedRandomAccessFile | BufferedOutputStream + DataOutputStream | 2.813 |
BufferedRandomAccessFile | BufferedRandomAccessFile | 2.453 |
BufferedRandomAccessFile優 | BufferedRandomAccessFile優 | 2.197 |
BufferedRandomAccessFile完 | BufferedRandomAccessFile完 | 0.401 |
MappedByteBuffer+ RandomAccessFile | MappedByteBuffer+ RandomAccessFile | 1.209 |
確實不錯,看來NIO有了極大的進步。建議採用 MappedByteBuffer+RandomAccessFile的方式。