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();  
                 

  • 相关阅读:
    备考C++有感
    使用GridView来获取xml文件数据
    SQL 事务及实例演示
    MySQL数据分析-(12)表操作补充:字段属性
    以字符串为例,谈谈Python到底要学到什么程度
    MySQL数据分析-(9)库操作补充:用户管理和权限管理
    Python流程控制和缩进
    MySQL数据分析-(11)表补充:数据类型
    MySQL数据分析-(8)SQL基础操作之库操作
    外键
  • 原文地址:https://www.cnblogs.com/onlywujun/p/2786131.html
Copyright © 2011-2022 走看看