3

第三步调:利用Java的FileChannel复制。

FileChannel的实例现实上仍是FileStreams,不外对其进行了包装机能上更高一下,也加倍便利一点。

代码如下:

引入架包:

import java.io.*;
import java.nio.channels.FileChannel;
public static void testFileChannel(){
File inFile = new File("E:/图片/捉妖记.jpg");
File outFile = new File("E:/file/捉妖记.jpg");
FileChannel inputChannel = null;
FileChannel outputChannel = null;    t
ry {
inputChannel = new FileInputStream(inFile).getChannel();
outputChannel = new FileOutputStream(outFile).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}}

java 复制功能 java实现复制_Java