Java NIO复制文件功能

  1. 	1. package com.ghb.crp.file;   
    	2.   
    	3. import java.io.BufferedInputStream;   
    	4. import java.io.BufferedOutputStream;   
    	5. import java.io.File;   
    	6. import java.io.FileInputStream;   
    	7. import java.io.FileOutputStream;   
    	8. import java.nio.ByteBuffer;   
    	9. import java.nio.channels.FileChannel;   
    	10.   
    	11. public class FileCopy {   
    	12.     public static String src = "f://ss.rmvb";   
    	13.     public static String dist = "f://ss2.rmvb";   
    	14.   
    	15.     /**  
    	16.      * @param args  
    	17.      */  
    	18.     public static void main(String[] args) {   
    	19.         long start = System.currentTimeMillis();   
    	20.         copyNew();   
    	21.         long end = System.currentTimeMillis();   
    	22.         System.out.println("nio 用时: " + (end - start));   
    	23.   
    	24.         long start2 = System.currentTimeMillis();   
    	25.         copyOld();   
    	26.         end = System.currentTimeMillis();   
    	27.         System.out.println("io 用时: " + (end - start2));   
    	28.     }   
    	29.   
    	30.     public static void copyOld() {   
    	31.         File srcFile = new File(src);   
    	32.         File distFile = new File(dist);   
    	33.         if (distFile.exists()) {   
    	34.             distFile.delete();   
    	35.         }   
    	36.         try {   
    	37.             FileInputStream fin = new FileInputStream(srcFile);   
    	38.             BufferedInputStream buffIn = new BufferedInputStream(fin);   
    	39.   
    	40.             FileOutputStream fout = new FileOutputStream(distFile);   
    	41.             BufferedOutputStream buffout = new BufferedOutputStream(fout);   
    	42.             byte[] bytes = new byte[1024];   
    	43.             while (buffIn.read(bytes) > 0) {   
    	44.                 buffout.write(bytes);   
    	45.             }   
    	46.             fin.close();   
    	47.             buffIn.close();   
    	48.             fout.close();   
    	49.             buffout.close();   
    	50.         } catch (Exception e) {   
    	51.             e.printStackTrace();   
    	52.         }   
    	53.   
    	54.     }   
    	55.   
    	56.     public static void copyNew() {   
    	57.         try {   
    	58.             File srcFile = new File(src);   
    	59.             File distFile = new File(dist);   
    	60.             if (distFile.exists()) {   
    	61.                 distFile.delete();   
    	62.             }   
    	63.   
    	64.             FileInputStream fin = new FileInputStream(srcFile);   
    	65.             FileOutputStream fout = new FileOutputStream(distFile);   
    	66.             FileChannel inChannel = fin.getChannel();   
    	67.             FileChannel outChannel = fout.getChannel();   
    	68.             int ByteBufferSize = 1024 * 100;   
    	69.             ByteBuffer buff = ByteBuffer.allocate(ByteBufferSize);   
    	70.   
    	71.             while (inChannel.read(buff) > 0) {   
    	72.                 buff.flip();   
    	73.                 if (inChannel.position() == inChannel.size()) {// 判断是不是最后一段数据   
    	74.                     int lastRead = (int) (inChannel.size() % ByteBufferSize);   
    	75.                     byte[] bytes = new byte[lastRead];   
    	76.                     buff.get(bytes, 0, lastRead);   
    	77.                     outChannel.write(ByteBuffer.wrap(bytes));   
    	78.                     buff.clear();   
    	79.                 } else {   
    	80.                     outChannel.write(buff);   
    	81.                     buff.clear();   
    	82.                 }   
    	83.             }// 这个使用FileChannel 自带的复制   
    	84.             // outChannel.transferFrom(inChannel, 0, inChannel.size());   
    	85.             outChannel.close();   
    	86.             inChannel.close();   
    	87.             fin.close();   
    	88.             fout.close();   
    	89.         } catch (Exception e) {   
    	90.             e.printStackTrace();   
    	91.         }   
    	92.     }   
    	93. }  
    
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyJiang.2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值