下面的代码实现使用映射文件复制文件,我复制的是1G多的文件
一次读进内存不够放,怎么才能读完这个文件?
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.DoubleBuffer;
import java.nio.channels.FileChannel;public class ByteBufferMap { public static void main(String[] args) {
FileChannel fcIn = null;
FileChannel fcOut = null;
try {
String filePath = "E:\\Animation\\AngelBeats\\1.mkv";
fcIn = new FileInputStream(new File(filePath))
.getChannel();
DoubleBuffer bufIn = fcIn.map(FileChannel.MapMode.READ_ONLY, 0,
fcIn.size()).asDoubleBuffer(); fcOut = new RandomAccessFile(new File("G:\\1.mkv"),
"rw").getChannel();
DoubleBuffer bufOut = fcOut.map(FileChannel.MapMode.READ_WRITE, 0,
fcIn.size()).asDoubleBuffer(); long start = System.currentTimeMillis();
double[] buf = new double[99999];
/*当get()返回的长度小于buf数组的时候就会出异常,我要怎么知道
get()还剩多少?*/
while (bufIn.hasRemaining()) {
bufIn.get(buf);
bufOut.put(buf);
}
long end = System.currentTimeMillis();
System.out.println("Time:" + ((double) ((end - start) / 1000))
+ "s");
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
fcIn.close();
fcOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
一次读进内存不够放,怎么才能读完这个文件?
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.DoubleBuffer;
import java.nio.channels.FileChannel;public class ByteBufferMap { public static void main(String[] args) {
FileChannel fcIn = null;
FileChannel fcOut = null;
try {
String filePath = "E:\\Animation\\AngelBeats\\1.mkv";
fcIn = new FileInputStream(new File(filePath))
.getChannel();
DoubleBuffer bufIn = fcIn.map(FileChannel.MapMode.READ_ONLY, 0,
fcIn.size()).asDoubleBuffer(); fcOut = new RandomAccessFile(new File("G:\\1.mkv"),
"rw").getChannel();
DoubleBuffer bufOut = fcOut.map(FileChannel.MapMode.READ_WRITE, 0,
fcIn.size()).asDoubleBuffer(); long start = System.currentTimeMillis();
double[] buf = new double[99999];
/*当get()返回的长度小于buf数组的时候就会出异常,我要怎么知道
get()还剩多少?*/
while (bufIn.hasRemaining()) {
bufIn.get(buf);
bufOut.put(buf);
}
long end = System.currentTimeMillis();
System.out.println("Time:" + ((double) ((end - start) / 1000))
+ "s");
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
fcIn.close();
fcOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileChannel fcIn = ……;
FileChannel fcOut = ……;
fcIn.transferTo(0,fcIn.size(),fcOut);