zoukankan      html  css  js  c++  java
  • Java NIO

    读取指定文件的类容:

    package com.nio;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class FileInputStreamTest {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File file = new File("d:\reader.txt");
            FileInputStream in = new FileInputStream(file);
            FileChannel channel = in.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
            
            channel.read(buffer);
            buffer.rewind();
            byte[] b = buffer.array();
            System.out.println(new String(b));
            
            channel.close();
            in.close();
        }
    
    }

    文件拷贝:

    package com.nio;
    
    import java.io.FileInputStream;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class CopyFileTest {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            FileInputStream fin = new FileInputStream("d:\reader.txt");
            FileOutputStream fout = new FileOutputStream("d:\nioTest.txt");
            FileChannel inChannel = fin.getChannel();
            FileChannel outChannel = fout.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(128);
            while(true){
                buffer.clear();
                int r = inChannel.read(buffer);
                if(r==-1){
                    break;
                }
                buffer.flip();
                outChannel.write(buffer);
            }
            outChannel.close();
            inChannel.close();
            fout.close();
            fin.close();
        }
    
    }

     参考文档:http://ifeve.com/buffers/

    socket:http://blog.csdn.net/kongxx/article/details/7288896

    http://developer.51cto.com/art/201112/306363.htm

  • 相关阅读:
    2016年总结,不一样的2016
    appium 遇到的坑
    Python xml 解析百度糯米信息
    Python 3.4 链接mysql5.7 数据库使用方法
    python3.x爬取美团信息
    基于python3的手机号生成脚本
    python3.x 学习心得
    H3C SNMP OID
    jython获取was5.1的jvm监控参数
    使用Jyhon脚本和PMI模块监控WAS性能数据
  • 原文地址:https://www.cnblogs.com/james-roger/p/5683948.html
Copyright © 2011-2022 走看看