Java中的NIO

2017-01-10  本文已影响0人  cp_insist

引言:NIO是Java面试中老生常谈的一个话题,No-Block-IO(非阻塞IO);今天专花了一天时间将并发变成网站上便于NIO的东西全看了一遍,下面算是自己的一个学习笔记,方便以后回顾:

一:NIO简介

二:具体用法:

public class deme1Nio {
    public static void main(String[] args) {
        File file = new File("F:\\1.txt");
        try {
            RandomAccessFile randomfile  = new RandomAccessFile(file, "rw");
            FileChannel channel = randomfile.getChannel();
            ByteBuffer buff = ByteBuffer.allocate(48);
            int data = channel.read(buff);
            while(data!=-1){
                //去读到文件的最后位置,安字节算的;换行:算两个字节
                int pos = buff.position();
                System.out.println(",pos="+pos);
                System.out.println(data);
                //切换到读模式
                buff.flip();
                while(buff.hasRemaining()){ 
                    byte[] bytes = new byte[pos];
                    buff.get(bytes);
                    System.out.print(new String(bytes,"GBK"));
                }
                buff.clear();
                data = channel.read(buff);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
File file1 = new File("f:\\1.txt"); 
        FileInputStream in1;
        try {
            in1 = new FileInputStream(file1);
            FileChannel channel1 = in1.getChannel();
             //用来存储头部信息
            ByteBuffer head = ByteBuffer.allocate(5);
             //用来存储区body信息
            ByteBuffer body = ByteBuffer.allocate(10);
            //将两个信息整合在一起
            ByteBuffer[] arr = {head,body};
            long data1 = channel1.read(arr);
            while(data1!=-1){
                head.flip();
                while(head.hasRemaining()){ 
                    byte[] he = new byte[head.limit()];
                    head.get(he);
                    System.out.println("头部信息为"+new String(he,"gbk"));
                }
                body.flip();
                while(body.hasRemaining()){
                    byte[] bo = new byte[body.limit()];
                    body.get(bo);
                    System.out.println("内容体信息为"+new String(bo,"gbk"));
                }
                head.compact();
                body.compact();
                data1 = channel1.read(arr);
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    public static void writeCombine(){
        File file1 = new File("f:\\2.txt"); 
        RandomAccessFile in1;
        try {
            in1 = new RandomAccessFile(file1,"rw");
             
            FileChannel channel1 = in1.getChannel();
             //用来存储头部信息
            ByteBuffer head = ByteBuffer.allocate(5);
            head.put("99999".getBytes());
             //用来存储区body信息
            ByteBuffer body = ByteBuffer.allocate(10);
            body.put("1111111111".getBytes());
            //将两个信息整合在一起
            ByteBuffer[] arr = {head,body};
            head.flip();
            body.flip();
            channel1.write(arr);
            channel1.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
public class demo3Transform {
    public static void trasfromF(){
        File  file1 = new File("F:\\1.txt");
        File  file2 = new File("F:\\2.txt");
        try {
            RandomAccessFile rfile1 = new RandomAccessFile(file1, "rw");
            RandomAccessFile rfile2 = new RandomAccessFile(file2, "rw");
            FileChannel channel1 = rfile1.getChannel();
            FileChannel channel2 = rfile2.getChannel();
            long position = channel2.position();
            long size = channel2.size();
            //将channel2的内容复制到channel1中去;(从1的position的位置开始)
            channel1.transferFrom(channel2, 2, size);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void trasfromT(){
        File  file1 = new File("F:\\1.txt");
        File  file2 = new File("F:\\2.txt");
        try {
            RandomAccessFile rfile1 = new RandomAccessFile(file1, "rw");
            RandomAccessFile rfile2 = new RandomAccessFile(file2, "rw");
            FileChannel channel1 = rfile1.getChannel();
            FileChannel channel2 = rfile2.getChannel();
            long position = channel2.position();
            long size = channel2.size();
            //将channel1的内容复制到channel2中去;(从2的position的位置开始,赋值2那么长)
            channel1.transferTo(position, size, channel2);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
      public static void main(String[] args) {
          demo3Transform.trasfromT();
      }
}
Set selectedKeys = selector.selectedKeys();
Iterator keyIterator = selectedKeys.iterator(); 
while(keyIterator.hasNext()) {    
    SelectionKey key = keyIterator.next();
if(key.isAcceptable())       
  // a connection was accepted by a ServerSocketChannel. 
} else if (key.isConnectable()) {
// a connection was established with a remote server.   
}else if (key.isReadable()) {
// a channel is ready for reading
} else if(key.isWritable()) {   
// a channel is ready for writing
}  
keyIterator.remove();
}

四:IO和NIO

1:区别:


IO和NIO的区别.png
上一篇 下一篇

猜你喜欢

热点阅读