Java NIO ByteBuffer讀取文件方式
Java NIO ByteBuffer讀取文件
FileChannel 和 ByteBuffer
從JDK1.4以后就提供java.nio的包,nio主要提供字節(jié)與字符的映射、內(nèi)存映射文件和文件加鎖機制
其中內(nèi)存映射文件在讀取大文件時可能會用上,因為內(nèi)存映射不是直接把文件加載到JVM內(nèi)存空間
而是借用操作系統(tǒng)對文件的讀取,這經(jīng)歷了由當前Java態(tài)進入到操作系統(tǒng)內(nèi)核態(tài),再由操作系統(tǒng)讀取文件,
并返回數(shù)據(jù)到當前Java態(tài)的過程。由Java態(tài)進入操作系統(tǒng)內(nèi)核態(tài)離不開nio包中兩個重要的類
FileChannel 和 ByteBuffer。FileChannel表示文件通道,可以從FileInputStream、FileOutputStream
以及RandomAccessFile對象獲取文件通道,你可以從文件通道直接讀取文件,也可以使用“內(nèi)存映射”
即使用通道,將文件內(nèi)存映射到ByteBuffer,可以映射一部分內(nèi)容,也可以映射全部內(nèi)容,使用內(nèi)存映射
能大幅提高我們操作大文件的速度
FileChannel 和 ByteBuffer文件讀取
package nio;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
/**
*
* Channel類似與流,數(shù)據(jù)可以從Channel讀取到Buffer,也可以從Buffer寫入到Channel
* 但通道和流還是有區(qū)別,比如流只能是單向讀或?qū)?而通道可以異步讀寫
*
* @author yli
*/
public class FileChannelTest {
// 110M
private static String file = "/mnt/hgfs/pdf/Java2核心技術II卷.高級特性.pdf";
public static void main(String[] args) throws IOException {
// 普通 NIO 讀取
// 每次讀取1024個字節(jié)
// readByChannelTest(1024); // 28151毫秒
// 普通 NIO 讀取
// 每次讀取1個字節(jié),每次讀取1個字節(jié)太慢了
// readByChannelTest(1);
// 使用內(nèi)存映射文件來讀取
// 從FileChannel拿到MappedByteBuffer,讀取文件內(nèi)容
readByChannelTest3(1024); // 61毫秒,甚至不到100毫秒
// 對于一個只有110M的文件,驗證使用FileChannel映射得到MappedByteBuffer
// 就能大幅提交文件讀取速度
// 普通的緩沖流讀取
// readByBufferdStream(); // 3922毫秒
}
/**
* 使用FileChannel讀取文件,并打印在控制臺
*
* @param 每次讀取多少個字節(jié)
* @throws IOException
*/
public static void readByChannelTest(int allocate) throws IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(file);
// 1.從FileInputStream對象獲取文件通道FileChannel
FileChannel channel = fis.getChannel();
long size = channel.size();
// 2.從通道讀取文件內(nèi)容
byte[] bytes = new byte[1024];
ByteBuffer byteBuffer = ByteBuffer.allocate(allocate);
// channel.read(ByteBuffer) 方法就類似于 inputstream.read(byte)
// 每次read都將讀取 allocate 個字節(jié)到ByteBuffer
int len;
while ((len = channel.read(byteBuffer)) != -1) {
// 注意先調(diào)用flip方法反轉(zhuǎn)Buffer,再從Buffer讀取數(shù)據(jù)
byteBuffer.flip();
// 有幾種方式可以操作ByteBuffer
// 1.可以將當前Buffer包含的字節(jié)數(shù)組全部讀取出來
//bytes = byteBuffer.array();
// System.out.print(new String(bytes));
// 2.類似與InputStrean的read(byte[],offset,len)方法讀取
byteBuffer.get(bytes, 0, len);
// System.out.print(new String(bytes, 0 ,len));
// 3.也可以遍歷Buffer讀取每個字節(jié)數(shù)據(jù)
// 一個字節(jié)一個字節(jié)打印在控制臺,但這種更慢且耗時
// while(byteBuffer.hasRemaining()) {
// System.out.print((char)byteBuffer.get());
// }
// 最后注意調(diào)用clear方法,將Buffer的位置回歸到0
byteBuffer.clear();
}
// 關閉通道和文件流
channel.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println(String.format("\n===>文件大?。?s 字節(jié)", size));
System.out.println(String.format("===>讀取并打印文件耗時:%s毫秒", end - start));
}
/**
* 仍然是根據(jù)FileChannel操作ByteBuffer,從ByteBuffer讀取內(nèi)容
* 通道讀取文件,速度比內(nèi)存映射慢很多,甚至比普通緩沖流要慢
*
* @param allocate
* @throws IOException
*/
public static void readByChannelTest2(int allocate) throws IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(file);
// 1.從FileInputStream對象獲取文件通道FileChannel
FileChannel channel = fis.getChannel();
long size = channel.size();
// 每次讀取allocate個字節(jié),計算要循環(huán)讀取多少次
long cycle = size / allocate;
// 看是否能整數(shù)倍讀完
int mode = (int) (size % allocate);
// 循環(huán)讀取
byte[] bytes;
ByteBuffer byteBuffer = ByteBuffer.allocate(allocate);
for (long i = 0; i < cycle; i++) {
if (channel.read(byteBuffer) != -1) {
byteBuffer.flip();
bytes = byteBuffer.array();
// System.out.print(new String(bytes));
byteBuffer.clear();
}
}
// 讀取最后mode個字節(jié)
if (mode > 0) {
byteBuffer = ByteBuffer.allocate(mode);
if (channel.read(byteBuffer) != -1) {
byteBuffer.flip();
bytes = byteBuffer.array();
// System.out.print(new String(bytes));
byteBuffer.clear();
}
}
// 關閉通道和文件流
channel.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println(String.format("\n===>文件大?。?s 字節(jié)", size));
System.out.println(String.format("===>讀取并打印文件耗時:%s毫秒", end - start));
}
/**
* 通過 FileChannel.map()拿到MappedByteBuffer
* 使用內(nèi)存文件映射,速度會快很多
*
* @throws IOException
*/
public static void readByChannelTest3(int allocate) throws IOException {
long start = System.currentTimeMillis();
RandomAccessFile fis = new RandomAccessFile(new File(file), "rw");
FileChannel channel = fis.getChannel();
long size = channel.size();
// 構建一個只讀的MappedByteBuffer
MappedByteBuffer mappedByteBuffer = channel.map(MapMode.READ_ONLY, 0, size);
// 如果文件不大,可以選擇一次性讀取到數(shù)組
// byte[] all = new byte[(int)size];
// mappedByteBuffer.get(all, 0, (int)size);
// 打印文件內(nèi)容
// System.out.println(new String(all));
// 如果文件內(nèi)容很大,可以循環(huán)讀取,計算應該讀取多少次
byte[] bytes = new byte[allocate];
long cycle = size / allocate;
int mode = (int)(size % allocate);
//byte[] eachBytes = new byte[allocate];
for (int i = 0; i < cycle; i++) {
// 每次讀取allocate個字節(jié)
mappedByteBuffer.get(bytes);
// 打印文件內(nèi)容,關閉打印速度會很快
// System.out.print(new String(eachBytes));
}
if(mode > 0) {
bytes = new byte[mode];
mappedByteBuffer.get(bytes);
// 打印文件內(nèi)容,關閉打印速度會很快
// System.out.print(new String(eachBytes));
}
// 關閉通道和文件流
channel.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println(String.format("\n===>文件大小:%s 字節(jié)", size));
System.out.println(String.format("===>讀取并打印文件耗時:%s毫秒", end - start));
}
/**
* 普通Java IO 緩沖流讀取
* @throws IOException
*/
public static void readByBufferdStream() throws IOException {
long start = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
long size = bis.available();
int len = 0;
int allocate = 1024;
byte[] eachBytes = new byte[allocate];
while((len = bis.read(eachBytes)) != -1) {
// System.out.print(new String(eachBytes, 0, len));
}
bis.close();
long end = System.currentTimeMillis();
System.out.println(String.format("\n===>文件大?。?s 字節(jié)", size));
System.out.println(String.format("===>讀取并打印文件耗時:%s毫秒", end - start));
}
}java nio ByteBuffer的使用
Buffer是nio包的一個抽象類,作為java nio的三大組件(Buffer、Channel,Selector)之一,在java nio網(wǎng)絡編程中尤為重要。
Buffer提供了一個字節(jié)緩沖區(qū),配合Channel使用,可以從Channel中讀取或?qū)懭霐?shù)據(jù)。
結構
屬性介紹
以ByteBuffer為例,其包括5個主要的屬性:hb、position、limit、capacity、mark。
hb:ByteBuffer類有一個byte數(shù)組變量hb,此數(shù)組里面存放的就是實際的字節(jié)數(shù)據(jù)。capacity:容量大小,其實就是hb字節(jié)數(shù)組的大小,始終不變。position:當前讀寫操作的ByteBuffer對象位置,其實就是hb字節(jié)數(shù)組下標位置。limit:讀寫操作position的大小限制,讀寫操作時position需要小于limit。mark:記錄當前讀寫的位置,方便后續(xù)使用。
常用API方法
/** * 初始化指定大小的ByteBuffer對象返回。 */ static ByteBuffer allocate(int capacity); /** * 使用指定字節(jié)數(shù)組初始化ByteBuffer對象返回。 */ static ByteBuffer wrap(byte[] array) /** * 返回當前position位置的字節(jié)數(shù)據(jù),position自增1。 */ byte get(); /** * 返回指定位置的數(shù)據(jù)。 */ byte get(int index); /** * 當前position位置設置為傳入字節(jié),position自增1。 */ ByteBuffer put(byte b); /** * 切換到讀模式。 */ Buffer flip(); /** * 切換到寫模式,不保留未讀完數(shù)據(jù)。 */ Buffer clear(); /** * 切換到寫模式,保留未讀完數(shù)據(jù)。 */ ByteBuffer compact();
圖解
①初始化狀態(tài)。執(zhí)行**ByteBuffer.allocate(10);**后狀態(tài)。

②調(diào)用put方法插入數(shù)據(jù),每往當前position位置插入一個數(shù)據(jù),position執(zhí)行加1操作。插入4個數(shù)據(jù)后狀態(tài)。

③調(diào)用flip方法,依次設置limit=position、position=0、mark=-1。

④調(diào)用get方法讀取數(shù)據(jù),返回當前position下標對應的值,然后positon執(zhí)行加1操作。讀取三個數(shù)據(jù)后狀態(tài)。

⑤調(diào)用clear或compact方法,重置position、limit的值。
調(diào)用clear后狀態(tài)(依次執(zhí)行position = 0、limit = capacity、mark = -1)。

調(diào)用compact后狀態(tài)。

讀模式和寫模式
其實ByteBuffer本身并沒有讀模式、寫模式的概念,為了便于初學者理解網(wǎng)友們強加的概念。
flip、clear、compact等方法只是修改了position、limit、mark等屬性的值而已,理解了上面的幾個操作圖就不需要理解不存在的讀模式、寫模式,避免混淆理解。
使用演示
- 演示一
/**
* 代碼
*/
public class ByteBufferDemo {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte)'a');
buffer.put((byte)'b');
buffer.put((byte)'c');
buffer.put((byte)'d');
System.out.println(buffer);
buffer.flip();
System.out.println(buffer);
System.out.println(buffer.get());
System.out.println(buffer.get());
System.out.println(buffer.get());
buffer.clear();
System.out.println(buffer);
System.out.println(Arrays.toString(buffer.array()));
System.out.println(buffer.get(2));
}
}
/**
* 運行結果
*/
java.nio.HeapByteBuffer[pos=4 lim=10 cap=10]
java.nio.HeapByteBuffer[pos=0 lim=4 cap=10]
97
98
99
java.nio.HeapByteBuffer[pos=0 lim=10 cap=10]
[97, 98, 99, 100, 0, 0, 0, 0, 0, 0]
99- 演示二
/**
* 代碼
*/
public class ByteBufferDemo {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte)'a');
buffer.put((byte)'b');
buffer.put((byte)'c');
buffer.put((byte)'d');
System.out.println(buffer);
buffer.flip();
System.out.println(buffer);
System.out.println(buffer.get());
System.out.println(buffer.get());
System.out.println(buffer.get());
buffer.compact();
System.out.println(buffer);
System.out.println(Arrays.toString(buffer.array()));
System.out.println(buffer.get(2));
}
}
/**
* 運行結果
*/
java.nio.HeapByteBuffer[pos=4 lim=10 cap=10]
java.nio.HeapByteBuffer[pos=0 lim=4 cap=10]
97
98
99
java.nio.HeapByteBuffer[pos=1 lim=10 cap=10]
[100, 98, 99, 100, 0, 0, 0, 0, 0, 0]
99- 演示三
/**
* 代碼
*/
public class ByteBufferDemo {
public static void main(String[] args) {
byte[] bytes = {(byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g'};
ByteBuffer buffer = ByteBuffer.wrap(bytes);
System.out.println(buffer);
System.out.println(Arrays.toString(buffer.array()));
//直接讀取數(shù)據(jù)
System.out.println(buffer.get());
System.out.println(buffer);
//寫入數(shù)據(jù)
buffer.put((byte)0x01);
buffer.put((byte)0x02);
System.out.println(buffer);
System.out.println(Arrays.toString(buffer.array()));
}
}
/**
* 運行結果
*/
java.nio.HeapByteBuffer[pos=0 lim=7 cap=7]
[97, 98, 99, 100, 101, 102, 103]
97
java.nio.HeapByteBuffer[pos=1 lim=7 cap=7]
java.nio.HeapByteBuffer[pos=3 lim=7 cap=7]
[97, 1, 2, 100, 101, 102, 103]總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot +MybatisPlus集成多數(shù)據(jù)源的使用案例
這篇文章主要介紹了SpringBoot +MybatisPlus集成多數(shù)據(jù)源的使用案例,本文分步驟結合實例代碼給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2025-05-05
java Bean與json對象間的轉(zhuǎn)換實例講解
在本篇文章里小編給大家整理的是關于java Bean與json間的轉(zhuǎn)換的實例內(nèi)容,有需要的朋友們吧可以學習參考下。2020-01-01
RocketMq同組消費者如何自動設置InstanceName
這篇文章主要介紹了RocketMq同組消費者如何自動設置InstanceName問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

