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

  • 相关阅读:
    mingw-gcc-10.0.1-experimental-i686-posix-sjlj-20200202-0303907
    可以修改 QtScrcpy 窗口大小的小工具
    autosub 添加代理服务器参数 -P --proxy
    Python网络数据采集系列-------概述
    【刷题笔记】I'm stuck! (迷宫)-----java方案
    【刷题笔记】火车购票-----java方案
    mvc自定义全局异常处理
    使用html2canvas实现浏览器截图
    再谈Newtonsoft.Json高级用法
    Spire.Doc组件读取与写入Word
  • 原文地址:https://www.cnblogs.com/onlywujun/p/2786131.html
Copyright © 2011-2022 走看看