nio fileChannel使用:
try(FileInputStream in=new FileInputStream("D:/soft/Evernote_6.5.4.4720.exe");
FileOutputStream out=new FileOutputStream("E:/Evernote_6.5.4.4720.exe");
FileChannel fcin=in.getChannel();
FileChannel fcou=out.getChannel()) {
fcou.transferFrom(fcin,0,fcin.size()); //使用transferFrom 直接将 fcin的数据输出到fc
}
try(FileInputStream in=new FileInputStream("D:/soft/Evernote_6.5.4.4720.exe");
FileOutputStream out=new FileOutputStream("E:/Evernote_6.5.4.4720.exe");
FileChannel fcin=in.getChannel();
FileChannel fcou=out.getChannel()) {
fcin.transferTo(0,fcin.size(),fcou); //使用transferTo 直接将 fcin的数据输出到fc
}
使用ByteBuffer写法:
try(FileInputStream in=new FileInputStream("D:/soft/Evernote_6.5.4.4720.exe");
FileOutputStream out=new FileOutputStream("E:/Evernote_6.5.4.4720.exe");
FileChannel fcin=in.getChannel();
FileChannel fcou=out.getChannel()) {
ByteBuffer bf=ByteBuffer.allocate(1024);
while(fcin.read(bf) != -1) {
bf.flip();
while(bf.hasRemaining()){
fcou.write(bf);
}
bf.clear();
}
}