zoukankan      html  css  js  c++  java
  • NIO学习:buffer读入与写出(文件复制示例)

         FileInputStream fInputStream=new FileInputStream(new File("/root/Desktop/testImage.jpg"));
            FileOutputStream fOutputStream =new FileOutputStream(new File("/root/Desktop/testImage2.jpg"));
            FileChannel fcIn=fInputStream.getChannel();
            FileChannel fcOut=fOutputStream.getChannel();
            ByteBuffer buffer=ByteBuffer.allocate(1024);
            //将管道的数据读入buffer中
            while(fcIn.read(buffer)!=-1){
                //由读操作转到写操作时必须调用flip来改变buffer的状态
                //内部操作:limit=position;position=0
                buffer.flip();
                //把buffer的数据写入管道
                fcOut.write(buffer);
                //清空buffer
                //内部操作:limit=capacity;position=0
                buffer.clear();
            }
            fcIn.close();
            fInputStream.close();
            fcOut.close();
            fOutputStream.close();
  • 相关阅读:
    文件同步
    Renesas PPP Mode
    PYTHON 常用API ***
    Python DB
    Python VIL Realse
    C BIN加密
    Python VIL Service Bin
    Python 定期检查Build_setting的编译情况
    Python 字串处理
    IP分片(IP Fragment)
  • 原文地址:https://www.cnblogs.com/DajiangDev/p/3921601.html
Copyright © 2011-2022 走看看