本帖最后由 张向辉 于 2013-1-26 16:41 编辑
下面一段使用FileChannel读写文件打印出来110毫秒:
public static void copyFile2(String sourceFile, String descFile) {
try {
long time = new Date().getTime();
FileChannel inChannel = new FileInputStream(sourceFile).getChannel();
FileChannel outChannel = new FileOutputStream(descFile).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
throw e;
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
long l = new Date().getTime() - time;
System.out.println("拷贝结束!----耗时:" + l + "毫秒");
} catch (Exception e) {
e.printStackTrace();
}
}
不使用FileChannel直接使用FileInputStream和FileOutputStream则快很多打印出来耗时0毫秒,这是为什么呢?
|