javaNio認(rèn)識(shí)與使用

nio是 java new io 的簡(jiǎn)寫 可以代替原來(lái) io所進(jìn)行的操作

old io 是面向流stream的
new io 是面向 緩沖區(qū)的

認(rèn)識(shí)nio 必須知道幾個(gè)重點(diǎn) 緩沖區(qū)buffer 通道channel

nio 讀取數(shù)據(jù)到緩沖區(qū)的兩種模式

  1. 直接緩沖區(qū) ( 通過(guò)allocatedircate()取得緩沖區(qū))
  2. 非直接緩沖區(qū)( 通過(guò) allocate()取得緩沖區(qū))
    區(qū)別:前者盡力不再內(nèi)存中創(chuàng)建副本,后者才副本中進(jìn)行實(shí)現(xiàn)
  • 非直接緩沖區(qū):通過(guò) allocate() 方法分配緩沖區(qū),將緩沖區(qū)建立在 JVM 的內(nèi)存中
  • 直接緩沖區(qū):通過(guò) allocateDirect() 方法分配直接緩沖區(qū),將緩沖區(qū)建立在物理內(nèi)存中??梢蕴岣咝?/li>

buffer使用

認(rèn)識(shí)五大關(guān)鍵 capacity limit position mark reset
構(gòu)造數(shù)據(jù)對(duì)象原子數(shù)據(jù)類型除了boolean以外都是可以的
bytebuffer,charbuffer,longbuffer''''''''''''''''''''''''''''''''''''''''''

Paste_Image.png

bytebuffer.allocate(1024);就會(huì)取得五個(gè)屬性

初始化position =0 limit=capacity=1024 mark記錄當(dāng)前位置 reset恢復(fù)position到mark標(biāo)記位置
0<=mark<=positon<=limit<=capacity

常用方法

Paste_Image.png

···
String str = "abcde";
//取得緩沖區(qū)
ByteBuffer buffer = ByteBuffer.allocate(1024);
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
System.out.println("--------------put---------------");
buffer.put(str.getBytes());
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
System.out.println("--------------filp讀寫---------------");
buffer.flip();
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
System.out.println("--------------get---------------");
byte[] dst=new byte[buffer.limit()];
buffer.get(dst);//取到了五個(gè)數(shù)據(jù)
System.out.println(new String(dst, 0, 2));
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
System.out.println("--------------reward---------------");
//重新執(zhí)行讀取,恢復(fù)位置
buffer.rewind();
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
System.out.println("--------------mark---------------");
System.out.println(buffer.mark());
System.out.println("--------------clear---------------");
//6. clear() : 清空緩沖區(qū). 但是緩沖區(qū)中的數(shù)據(jù)依然存在,但是處于“被遺忘”狀態(tài)
buffer.clear();
System.out.println((char)buffer.get());
}
···

···
String str = "abcde";

    ByteBuffer buf = ByteBuffer.allocate(1024);

    buf.put(str.getBytes());

    buf.flip();

    byte[] dst = new byte[buf.limit()];
    buf.get(dst, 0, 2);
    System.out.println(new String(dst, 0, 2));
    System.out.println(buf.position());

    // mark() : 標(biāo)記
    buf.mark();

    buf.get(dst, 2, 2);
    System.out.println(new String(dst, 2, 2));
    System.out.println(buf.position());

    // reset() : 恢復(fù)到 mark 的位置
    buf.reset();
    System.out.println(buf.position());

    // 判斷緩沖區(qū)中是否還有剩余數(shù)據(jù)
    if (buf.hasRemaining()) {

        // 獲取緩沖區(qū)中可以操作的數(shù)量
        System.out.println(buf.remaining());
    }

···

channel 通道

分散:從buffer讀取數(shù)據(jù)到多個(gè) buffer文件

聚集:從多個(gè)buffer讀取數(shù)據(jù)到channel

獲取channel getchannel()

filechanel 文件datagramchannel udp serversocketchannel tcp

socketchannel

類:
fileinputstream /fileoutputstream randomaccessfile datagram serversocket
socket

jdk 1.7 支持 openchannel() files 下 newbytechannel

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)自 http://www.ibm.com/developerworks/cn/education/java/j-...
    抓兔子的貓閱讀 2,340評(píng)論 0 22
  • Buffer java NIO庫(kù)是在jdk1.4中引入的,NIO與IO之間的第一個(gè)區(qū)別在于,IO是面向流的,而NI...
    德彪閱讀 2,232評(píng)論 0 3
  • Java NIO Buffer當(dāng)我們需要與NIO Channel進(jìn)行交互時(shí),我們就需要使用到NIO Buffer,...
    水欣閱讀 651評(píng)論 0 0
  • 遖慕迦閱讀 203評(píng)論 0 0
  • 我開始討厭很多東西。 討厭=羨慕和嫉妒,我沒(méi)有的別人有的,我有的別人也有的,我希望有的別人已經(jīng)有的。 ...
    云晝閱讀 166評(píng)論 0 1