transferTo
この方法で一見 1000 MB/秒以上の速度でファイルをコピーできる方法を説明してください。372MB のバイナリ ファイルを使用していくつかのテストを実行しましたが、最初のコピーは低速ですが、出力名を変更して再度実行すると、出力ディレクトリに追加のファイルがわずか 180 ミリ秒で表示され、結果として 2000 MB/ を超えます。秒 何が起きてる?私はWindows 7を実行しています。
private static void doCopyNIO(String inFile, String outFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel cis = null;
FileChannel cos = null;
long len = 0, pos = 0;
try {
fis = new FileInputStream(inFile);
cis = fis.getChannel();
fos = new FileOutputStream(outFile);
cos = fos.getChannel();
len = cis.size();
while (pos < len) {
pos += cis.transferTo(pos, (1024 * 1024 * 10), cos); // 10M
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cos != null) { try { cos.close(); } catch (Exception e) { } }
if (fos != null) { try { fos.close(); } catch (Exception e) { } }
if (cis != null) { try { cis.close(); } catch (Exception e) { } }
if (fis != null) { try { fis.close(); } catch (Exception e) { } }
}
}