public static void copyFileByNIO(String fromFile,String toFile){
FileInputStream in = null;
FileOutputStream out = null;
FileChannel fchannel = null;
FileChannel fcout = null;
try {
in = new FileInputStream(new File(fromFile));
out = new FileOutputStream(new File(toFile));
fchannel = in.getChannel();
fcout = out.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(true){
buffer.clear();
int r = fchannel.read(buffer);
if(r==-1){
break;
}
buffer.flip();
fcout.write(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
if(fchannel!=null){
fchannel.close();
}
if(fcout!=null){
fcout.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream in = null;
FileOutputStream out = null;
FileChannel fchannel = null;
FileChannel fcout = null;
try {
in = new FileInputStream(new File(fromFile));
out = new FileOutputStream(new File(toFile));
fchannel = in.getChannel();
fcout = out.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(true){
buffer.clear();
int r = fchannel.read(buffer);
if(r==-1){
break;
}
buffer.flip();
fcout.write(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
if(fchannel!=null){
fchannel.close();
}
if(fcout!=null){
fcout.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
333

被折叠的 条评论
为什么被折叠?



