码迷,mamicode.com
首页 > 编程语言 > 详细

java----使用NIO进行快速的文件拷贝

时间:2018-03-22 00:23:56      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:tst   article   tail   finally   null   files   color   div   public   

public static void fileCopy( File in, File out )  
            throws IOException  
    {  
        FileChannel inChannel = new FileInputStream( in ).getChannel();  
        FileChannel outChannel = new FileOutputStream( out ).getChannel();  
        try 
        {  
//          inChannel.transferTo(0, inChannel.size(), outChannel);      // original -- apparently has trouble copying large files on Windows  

            // magic number for Windows, 64Mb - 32Kb)  
            int maxCount = (64 * 1024 * 1024) - (32 * 1024);  
            long size = inChannel.size();  
            long position = 0;  
            while ( position < size )  
            {  
               position += inChannel.transferTo( position, maxCount, outChannel );  
            }  
        }  
        finally 
        {  
            if ( inChannel != null )  
            {  
               inChannel.close();  
            }  
            if ( outChannel != null )  
            {  
                outChannel.close();  
            }  
        }  
    }

使用NIO进行快速的文件拷贝  源自http://blog.csdn.net/just3do/article/details/68059647

java----使用NIO进行快速的文件拷贝

标签:tst   article   tail   finally   null   files   color   div   public   

原文地址:https://www.cnblogs.com/jahnson/p/8620697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!