第三步骤:使用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();
}
}}