zoukankan      html  css  js  c++  java
  • Nio最简单的Chanel应用

            String infile = "e:\\copy_i.txt"; 
            String outfile = "e:\\copy_o.txt"; 

        FileInputStream fin = new FileInputStream(infile); 
            FileOutputStream fout = new FileOutputStream(outfile);  
             
            FileChannel fcin = fin.getChannel(); 
            FileChannel fcout = fout.getChannel();     

            ByteBuffer buffer = ByteBuffer.allocate(1024);  
            while (true) {  
                  
                buffer.clear();          

                int r = fcin.read(buffer);   (从in流中读入到缓存区中)
                  
                if (r == -1) { 
                    break; 
                }  
                

        buffer.flip();  (flip方法把limit值=position值,position值=0,用来反向写出)
                fcout.write(buffer);  (从缓存区中写出到out流中)
            }  
    -----------------------------------------------------------

                InetSocketAddress socketAddress = new InetSocketAddress("www.baidu.com", 80);   
                SocketChannel channel = SocketChannel.open(socketAddress);  
                channel.write(charset.encode("GET " + "/ HTTP/1.1" + "\r\n\r\n"));    
                ByteBuffer buffer = ByteBuffer.allocate(1024);  
                while (channel.read(buffer) != -1) { 
                    buffer.flip();   
                    System.out.println(Charset.forName("GBK").decode(buffer));  
                    buffer.clear();// 清空缓冲

        }   
                    

        channel.close();  
                 

  • 相关阅读:
    hibernate连接MySQL配置hibernate.cfg.xml
    行百里者半九十 —— 查找算法
    行百里者半九十 —— 链表(1)
    行百里者半九十 —— 查找算法(中等)
    行百里者半九十 —— 字符串
    设计模式 —— 总结
    并发编程 —— 使用条件变量构建线程安全队列
    行百里者半九十 ——栈与队列
    设计模式 —— 命令模式
    css选择器中:firstchild与:firstoftype的区别
  • 原文地址:https://www.cnblogs.com/onlywujun/p/2786131.html
Copyright © 2011-2022 走看看