亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

NIO的三大核心配件

Java NIO的三大核心配件:通道、緩沖區(qū)、選擇器

 

channel(通道)

Buffer(緩沖區(qū))

selector(選擇器)

 

案例介紹

讀寫切換

 

public static void main(String[] args) {
    //創(chuàng)建一個Buffer,大小為5,可就是可以存放5個int
 IntBuffer intBuffer=IntBuffer.allocate(5);
 for(int i=0;i<intBuffer.capacity();i++){
        intBuffer.put(i*2);
 }
    //讀寫切換
 intBuffer.flip();
 while(intBuffer.hasRemaining()){
        System.out.println(intBuffer.get());
 }
}

本地方法寫

public static void main(String[] args) throws IOException {
 String str="hello";
 //創(chuàng)建一個輸出流
 FileOutputStream fileOutputStream=new FileOutputStream("src/test/JAVA/com/hua/nio/a");
 //通過fileOutputStream獲取對應的FileChannel,這個FielChannel的真實類型是FileChannelImpl
 FileChannel fileChannel=fileOutputStream.getChannel();
 //創(chuàng)建一個緩沖區(qū)ByteBuffer
 ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
 //將str放到byteBuffer
 byteBuffer.put(str.getBytes());
 //對byteBuffer.flip();
 byteBuffer.flip();
 //把byteBuffer的數(shù)據(jù)寫入到fileChannel
 fileChannel.write(byteBuffer);
 fileOutputStream.close();
}

本地方法讀

public static void main(String[] args) throws IOException {
    File file=new File("src/test/java/com/hua/nio/a");
 FileInputStream fileInputStream =new FileInputStream(file);
 FileChannel fileChannel=fileInputStream.getChannel();
 ByteBuffer byteBuffer= ByteBuffer.allocate((int)file.length());
 //將通道的數(shù)據(jù)讀入到Buffer
 fileChannel.read(byteBuffer);
 //將byteBuffer的字節(jié)數(shù)據(jù)轉(zhuǎn)成String
 System.out.println(new String(byteBuffer.array()));
 fileInputStream.close();
}

數(shù)據(jù)的雙向交互

 

public static void main(String[] args) throws IOException {
    FileInputStream fileInputStream=new FileInputStream("src/test/java/com/hua/nio/a");
 FileOutputStream fileOutputStream=new FileOutputStream("src/test/java/com/hua/nio/b");
 FileChannel fileChannel1=fileInputStream.getChannel();
 FileChannel fileChannel2=fileOutputStream.getChannel();
 ByteBuffer byteBuffer= ByteBuffer.allocate(512);
 while(true){
        //必須要clear,當下面fileChannel2的write的時候,此時的position會從0開始到limit,如果不clear回歸到原來,
 //當position和limit相等的時候,fileChannel1.read的返回結(jié)果為-1
 byteBuffer.clear();
 int read=fileChannel1.read(byteBuffer);
 System.out.println(read);
 if(read == -1){
            break;
 }
        byteBuffer.flip();
 fileChannel2.write(byteBuffer);
 }
    fileInputStream.close();
 fileOutputStream.close();
}

通道的數(shù)據(jù)轉(zhuǎn)換

public static void main(String[] args) throws IOException {
    FileInputStream fileInputStream =new FileInputStream("src/test/java/com/hua/nio/a");
 FileOutputStream fileOutputStream=new FileOutputStream("src/test/java/com/hua/nio/c");
 FileChannel fileChannel1=fileInputStream.getChannel();
 FileChannel fileChannel2=fileOutputStream.getChannel();
 fileChannel2.transferFrom(fileChannel1,0,fileChannel1.size());
 fileChannel1.close();
 fileChannel2.close();
 fileInputStream.close();
 fileOutputStream.close();
}

類型的buffer

public static void main(String[] args) {
    ByteBuffer buffer= ByteBuffer.allocate(64);
 buffer.putInt(100);
 buffer.putLong(5);
 buffer.putChar('天');
 buffer.putShort((short)11);
 buffer.flip();
 System.out.println();
 System.out.println(buffer.getInt());
 System.out.println(buffer.getLong());
 System.out.println(buffer.getChar());
 System.out.println(buffer.getShort());
}

只讀的buffer

public static void main(String[] args) {
    ByteBuffer buffer= ByteBuffer.allocate(64);
 for(int i=0;i<64;i++){
        buffer.put((byte)i);
 }
    buffer.flip();
 //設置byteBuffer是只讀的
 ByteBuffer readOnlyBuffer=buffer.asReadOnlyBuffer();
 System.out.println(readOnlyBuffer.getClass());
 while(readOnlyBuffer.hasRemaining()){
        System.out.println(readOnlyBuffer.get());
 }
}

內(nèi)存上的修改

public static void main(String[] args) throws IOException {
    RandomaccessFile randomAccessFile=new RandomAccessFile("src/test/java/com/hua/nio/a","rw");
 FileChannel channel=randomAccessFile.getChannel();
 //FileChannel.MapMode.READ_WRITR使用的是讀寫模式
 //第一個參數(shù)表示直接修改的起始位置
 //第二個參數(shù)表示映射到內(nèi)存的大小
 //也就是說我們只能修改0到4的索引位置,
 MAppedByteBuffer mappedByteBuffer=channel.map(FileChannel.MapMode.READ_WRITE,0,5);
 mappedByteBuffer.put(0,(byte)'H');
 mappedByteBuffer.put(2,(byte)'L');
 randomAccessFile.close();
}

分享到:
標簽:Java NIO
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定