java代码粘贴后报错_java – 尝试复制大文件时NIO出错

我有代码将文件复制到另一个位置.

public static void copyFile(String sourceDest, String newDest) throws IOException {

File sourceFile = new File(sourceDest);

File destFile = new File(newDest);

if (!destFile.exists()) {

destFile.createNewFile();

}

FileChannel source = null;

FileChannel destination = null;

try {

source = new FileInputStream(sourceFile).getChannel();

destination = new FileOutputStream(destFile).getChannel();

destination.transferFrom(source, 0, source.size());

} finally {

if (source != null) {

source.close();

}

if (destination != null) {

destination.close();

}

}

}

}

虽然复制小块,比如300-400 Mb,但一切都像魔术一样.但是,当我尝试复制1.5 Gb大小的文件时,它失败了.堆栈是:

run:

12.01.2011 11:16:36 FileCopier main

SEVERE: Exception occured while copying file. Try again.

java.io.IOException: Map failed

at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:748)

at sun.nio.ch.FileChannelImpl.transferFromFileChannel(FileChannelImpl.java:527)

at sun.nio.ch.FileChannelImpl.transferFrom(FileChannelImpl.java:590)

at FileCopier.copyFile(FileCopier.java:64)

at FileCopier.main(FileCopier.java:27)

Caused by: java.lang.OutOfMemoryError: Map failed

at sun.nio.ch.FileChannelImpl.map0(Native Method)

at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:745)

… 4 more

BUILD SUCCESSFUL (total time: 0 seconds)

我没有和NIO密切合作过.你能帮帮我吗?非常感谢你提前.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值