zoukankan      html  css  js  c++  java
  • nio

    jdk1.4中引入lava.nio.*,使用通道和缓冲器提高速度:体现在文件IO和网络IO

    通道(Channel):即数据

    缓冲器:承载一定大小的数据

    唯一与通道交互的缓冲器是ByteBuffer

    文件IO中的应用:

    public class ChannelCopy {
      private static final int BSIZE = 1024;
      public static void main(String[] args) throws Exception {
        if(args.length != 2) {
          System.out.println("arguments: sourcefile destfile");
          System.exit(1);
        }
        FileChannel
          in = new FileInputStream(args[0]).getChannel(),
          out = new FileOutputStream(args[1]).getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
        while(in.read(buffer) != -1) {
          buffer.flip(); // Prepare for writing
          out.write(buffer);
          buffer.clear();  // Prepare for reading
        }
      }
    } 

    还可以将通道直接相连:

    public class TransferTo {
      public static void main(String[] args) throws Exception {
        if(args.length != 2) {
          System.out.println("arguments: sourcefile destfile");
          System.exit(1);
        }
        FileChannel
          in = new FileInputStream(args[0]).getChannel(),
          out = new FileOutputStream(args[1]).getChannel();
        in.transferTo(0, in.size(), out);
        // Or:
        // out.transferFrom(in, 0, in.size());
      }
    }

    网络IO中的应用:

    http://blog.csdn.net/anxpp/article/details/51512200

    https://my.oschina.net/hosee/blog/615269

    java7中新的异步通道:

    AsynchronousFileChannel--文件io

    AsynchronousSocketChannel--套接字io

    AsynchronousServerSocketChannel--用于套接字接受异步连接

    将来式与回调式

    NetworkChannel

    MulticastChannel

  • 相关阅读:
    JSP获取input(含正则表达式)
    Computability 7: Exercises
    Network 5: Data Link Layer
    PRML 7: The EM Algorithm
    PRML 6: SVD and PCA
    PRML 5: Kernel Methods
    PRML 4: Generative Models
    Computability 6: Reducibility
    Distributed Hash Table
    Network 4: Network Layer
  • 原文地址:https://www.cnblogs.com/qilong853/p/6518502.html
Copyright © 2011-2022 走看看