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# 调用cmd执行指令
    如何发布 silverlight wcf 简单易学
    C#读取特定目录下的所有文件
    用批处理bat一次安装所有的系统更新补丁
    动态创建datagrid序号
    学习DIV+CSS一个最简单的布局一行三列DIV代码!
    highslide图片查看特效
    相册程序mageVue
    让Apache支持ASP.NET
    ASP.NET四种页面导航方式之比较与选择
  • 原文地址:https://www.cnblogs.com/onlywujun/p/2786131.html
Copyright © 2011-2022 走看看