Java NIO ByteBuffer讀取文件方式
Java NIO ByteBuffer讀取文件
FileChannel 和 ByteBuffer
從JDK1.4以后就提供java.nio的包,nio主要提供字節(jié)與字符的映射、內(nèi)存映射文件和文件加鎖機(jī)制
其中內(nèi)存映射文件在讀取大文件時(shí)可能會(huì)用上,因?yàn)閮?nèi)存映射不是直接把文件加載到JVM內(nèi)存空間
而是借用操作系統(tǒng)對(duì)文件的讀取,這經(jīng)歷了由當(dāng)前Java態(tài)進(jìn)入到操作系統(tǒng)內(nèi)核態(tài),再由操作系統(tǒng)讀取文件,
并返回?cái)?shù)據(jù)到當(dāng)前Java態(tài)的過程。由Java態(tài)進(jìn)入操作系統(tǒng)內(nèi)核態(tài)離不開nio包中兩個(gè)重要的類
FileChannel 和 ByteBuffer。FileChannel表示文件通道,可以從FileInputStream、FileOutputStream
以及RandomAccessFile對(duì)象獲取文件通道,你可以從文件通道直接讀取文件,也可以使用“內(nèi)存映射”
即使用通道,將文件內(nèi)存映射到ByteBuffer,可以映射一部分內(nèi)容,也可以映射全部?jī)?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核心技術(shù)II卷.高級(jí)特性.pdf";
public static void main(String[] args) throws IOException {
// 普通 NIO 讀取
// 每次讀取1024個(gè)字節(jié)
// readByChannelTest(1024); // 28151毫秒
// 普通 NIO 讀取
// 每次讀取1個(gè)字節(jié),每次讀取1個(gè)字節(jié)太慢了
// readByChannelTest(1);
// 使用內(nèi)存映射文件來讀取
// 從FileChannel拿到MappedByteBuffer,讀取文件內(nèi)容
readByChannelTest3(1024); // 61毫秒,甚至不到100毫秒
// 對(duì)于一個(gè)只有110M的文件,驗(yàn)證使用FileChannel映射得到MappedByteBuffer
// 就能大幅提交文件讀取速度
// 普通的緩沖流讀取
// readByBufferdStream(); // 3922毫秒
}
/**
* 使用FileChannel讀取文件,并打印在控制臺(tái)
*
* @param 每次讀取多少個(gè)字節(jié)
* @throws IOException
*/
public static void readByChannelTest(int allocate) throws IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(file);
// 1.從FileInputStream對(duì)象獲取文件通道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 個(gè)字節(jié)到ByteBuffer
int len;
while ((len = channel.read(byteBuffer)) != -1) {
// 注意先調(diào)用flip方法反轉(zhuǎn)Buffer,再?gòu)腂uffer讀取數(shù)據(jù)
byteBuffer.flip();
// 有幾種方式可以操作ByteBuffer
// 1.可以將當(dāng)前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讀取每個(gè)字節(jié)數(shù)據(jù)
// 一個(gè)字節(jié)一個(gè)字節(jié)打印在控制臺(tái),但這種更慢且耗時(shí)
// while(byteBuffer.hasRemaining()) {
// System.out.print((char)byteBuffer.get());
// }
// 最后注意調(diào)用clear方法,將Buffer的位置回歸到0
byteBuffer.clear();
}
// 關(guān)閉通道和文件流
channel.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println(String.format("\n===>文件大?。?s 字節(jié)", size));
System.out.println(String.format("===>讀取并打印文件耗時(shí):%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對(duì)象獲取文件通道FileChannel
FileChannel channel = fis.getChannel();
long size = channel.size();
// 每次讀取allocate個(gè)字節(jié),計(jì)算要循環(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個(gè)字節(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();
}
}
// 關(guān)閉通道和文件流
channel.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println(String.format("\n===>文件大?。?s 字節(jié)", size));
System.out.println(String.format("===>讀取并打印文件耗時(shí):%s毫秒", end - start));
}
/**
* 通過 FileChannel.map()拿到MappedByteBuffer
* 使用內(nèi)存文件映射,速度會(huì)快很多
*
* @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();
// 構(gòu)建一個(gè)只讀的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)讀取,計(jì)算應(yīng)該讀取多少次
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個(gè)字節(jié)
mappedByteBuffer.get(bytes);
// 打印文件內(nèi)容,關(guān)閉打印速度會(huì)很快
// System.out.print(new String(eachBytes));
}
if(mode > 0) {
bytes = new byte[mode];
mappedByteBuffer.get(bytes);
// 打印文件內(nèi)容,關(guān)閉打印速度會(huì)很快
// System.out.print(new String(eachBytes));
}
// 關(guān)閉通道和文件流
channel.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println(String.format("\n===>文件大?。?s 字節(jié)", size));
System.out.println(String.format("===>讀取并打印文件耗時(shí):%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("===>讀取并打印文件耗時(shí):%s毫秒", end - start));
}
}java nio ByteBuffer的使用
Buffer是nio包的一個(gè)抽象類,作為java nio的三大組件(Buffer、Channel,Selector)之一,在java nio網(wǎng)絡(luò)編程中尤為重要。
Buffer提供了一個(gè)字節(jié)緩沖區(qū),配合Channel使用,可以從Channel中讀取或?qū)懭霐?shù)據(jù)。
結(jié)構(gòu)
屬性介紹
以ByteBuffer為例,其包括5個(gè)主要的屬性:hb、position、limit、capacity、mark。
hb:ByteBuffer類有一個(gè)byte數(shù)組變量hb,此數(shù)組里面存放的就是實(shí)際的字節(jié)數(shù)據(jù)。capacity:容量大小,其實(shí)就是hb字節(jié)數(shù)組的大小,始終不變。position:當(dāng)前讀寫操作的ByteBuffer對(duì)象位置,其實(shí)就是hb字節(jié)數(shù)組下標(biāo)位置。limit:讀寫操作position的大小限制,讀寫操作時(shí)position需要小于limit。mark:記錄當(dāng)前讀寫的位置,方便后續(xù)使用。
常用API方法
/** * 初始化指定大小的ByteBuffer對(duì)象返回。 */ static ByteBuffer allocate(int capacity); /** * 使用指定字節(jié)數(shù)組初始化ByteBuffer對(duì)象返回。 */ static ByteBuffer wrap(byte[] array) /** * 返回當(dāng)前position位置的字節(jié)數(shù)據(jù),position自增1。 */ byte get(); /** * 返回指定位置的數(shù)據(jù)。 */ byte get(int index); /** * 當(dāng)前position位置設(shè)置為傳入字節(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ù),每往當(dāng)前position位置插入一個(gè)數(shù)據(jù),position執(zhí)行加1操作。插入4個(gè)數(shù)據(jù)后狀態(tài)。

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

④調(diào)用get方法讀取數(shù)據(jù),返回當(dāng)前position下標(biāo)對(duì)應(yīng)的值,然后positon執(zhí)行加1操作。讀取三個(gè)數(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)。

讀模式和寫模式
其實(shí)ByteBuffer本身并沒有讀模式、寫模式的概念,為了便于初學(xué)者理解網(wǎng)友們強(qiáng)加的概念。
flip、clear、compact等方法只是修改了position、limit、mark等屬性的值而已,理解了上面的幾個(gè)操作圖就不需要理解不存在的讀模式、寫模式,避免混淆理解。
使用演示
- 演示一
/**
* 代碼
*/
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));
}
}
/**
* 運(yùn)行結(jié)果
*/
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));
}
}
/**
* 運(yùn)行結(jié)果
*/
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()));
}
}
/**
* 運(yùn)行結(jié)果
*/
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]總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot +MybatisPlus集成多數(shù)據(jù)源的使用案例
這篇文章主要介紹了SpringBoot +MybatisPlus集成多數(shù)據(jù)源的使用案例,本文分步驟結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-05-05
java Bean與json對(duì)象間的轉(zhuǎn)換實(shí)例講解
在本篇文章里小編給大家整理的是關(guān)于java Bean與json間的轉(zhuǎn)換的實(shí)例內(nèi)容,有需要的朋友們吧可以學(xué)習(xí)參考下。2020-01-01
RocketMq同組消費(fèi)者如何自動(dòng)設(shè)置InstanceName
這篇文章主要介紹了RocketMq同組消費(fèi)者如何自動(dòng)設(shè)置InstanceName問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java面向?qū)ο笾甪inal關(guān)鍵字詳細(xì)解讀
這篇文章主要介紹了Java面向?qū)ο笾甪inal關(guān)鍵字詳細(xì)解讀,final修飾的屬性又叫常量,一般用 XX_XX_XX來命名,final修飾的屬性在定義時(shí)必須賦初始值,并且以后不能再修改,需要的朋友可以參考下2024-01-01
java學(xué)習(xí)之jar包的下載和導(dǎo)入
我們經(jīng)常碰到有些jar包在中央倉(cāng)庫(kù)沒有的情況,這時(shí)候我們需要導(dǎo)入,這篇文章主要給大家介紹了關(guān)于java學(xué)習(xí)之jar包的下載和導(dǎo)入的相關(guān)資料,需要的朋友可以參考下2023-06-06

