zoukankan      html  css  js  c++  java
  • NioCopy文件

    步骤:

    1.创建输入输出流  fis fos

    2.创建通道  fis.getchannel()  fos.getchannel();

    3.创建缓存区      ByteBuffer buffer = ByteBuffer.allocate(1024);

    4.遍历缓存区

      buffer.clear();

      readeChannel.read(buffer);

      buffer.flip();

     writeChannel.write(channel);

    package com.somp.NioCopy;

    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;

    /**
     * Nio文件copy
     * @author Administrator
     *
     */
    public class NioCopy {
        /**
         * 1.初始化缓存区
         * 2.获取通道
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
            //读操作
            FileInputStream fileInputStream = new FileInputStream("C:\HomeStatisticsServiceImpl.java");
            //写操作
            FileOutputStream fos = new FileOutputStream("abc.java");
            //获取通道
            FileChannel readChannel = fileInputStream.getChannel();
            FileChannel writeChannel = fos.getChannel();
            //声明缓存区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (true) {
                buffer.clear();
                try {
                    //通道开始读取缓存区
                    int read = readChannel.read(buffer);
                    if(read==-1){
                        break;
                    }else{
                        buffer.flip();
                    }
                        writeChannel.write(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            fos.close();
            fileInputStream.close();
        }

    }

  • 相关阅读:
    Win7 SP1 安装SQL Server 2012时提示“此计算机上的操作系统不符合 SQL Server 2012的最低要求”
    ajax jsonp跨域
    Caused by: Unable to locate parent package [json-package] for [class com.you.action.ColumnAction]
    PHP MVC自己主动RBAC自己主动生成的访问路由
    Service与Activity与交流AIDL
    SVN常见错误两项纪录
    EL表达式语言
    oracle11g ASM(修复损坏的磁盘组头asm修复2)
    如何使用iOS 8 指纹识别,代码、示例
    EXCEL Pivot table manipulate
  • 原文地址:https://www.cnblogs.com/gyadmin/p/8435357.html
Copyright © 2011-2022 走看看