关于MappedByteBuffer内存映射的问题!!!急

hgs325 2007-08-31 12:50:43
public class FileCopy {
public static boolean copy(File src, File news) throws IOException {
boolean is = false;
FileChannel in = new RandomAccessFile(src, "rw").getChannel(), out = new RandomAccessFile(
news, "rw").getChannel();
ByteBuffer input = in.map(FileChannel.MapMode.READ_WRITE, 0, in.size()), output = out
.map(FileChannel.MapMode.READ_WRITE, 0, in.size());
while (input.hasRemaining()) {
output.put(input.get());
}
in.close();
out.close();
is = true;
return is;
}

public static void main(String[] args) throws IOException {
File old = new File("g:\\bb.exe");
File news = new File("g:\\cc.exe");
if (FileCopy.copy(old, news)) {
JOptionPane.showMessageDialog(null, "复制成功!");
} else {
JOptionPane.showMessageDialog(null, "复制失败!");
}
}
}

上面是我的代码,问题是如果文件很大比如2G,那么调用size()返回的数字大于内存就会有内存不足异常,没有那么大的内存怎么办?如何修改能做到他动态内存映射?
...全文
231 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧