关于MappedByteBuffer内存映射的问题!!!急
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()返回的数字大于内存就会有内存不足异常,没有那么大的内存怎么办?如何修改能做到他动态内存映射?