把一个较大的文件(G级)复制到其它文件夹中

//把一个较大的文件(G以上)复制到其它文件夹中
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class CopyBigFile {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	//声明流,不初始化
	FileChannel inChannel = null;
	FileChannel outChannel = null;
	
	try {
		//初始化流
		//读取大文件,第一个参数是文件路径,第二个参数是读取权限
		inChannel = new RandomAccessFile("e:\\1、Object类.wmv", "r").getChannel();
		//写入大文件
		outChannel = new RandomAccessFile("d:\\obj.wmv", "rw").getChannel();
		//把内存映射到缓冲区
		MappedByteBuffer map = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
		//把缓冲区的内容通过输出管道写到指定路径中(并重新命名)
		outChannel.write(map);
		System.out.println("读取结束!");
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
		//通过判断关闭通道
		try {
			//通道不等于空说明通道被打开了,防止空指针异常
			if(outChannel!=null) {
				outChannel.close();
			}
			if(inChannel!=null) {
				inChannel.close();
			}
			
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
	
  }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值