3

我正在使用 CentOs 内核版本 2.6.32。我计划使用 NIO 进行有无 transferTo(sendFile) 的测试。我的测试是将一个 1GB 的文件从一个目录复制到另一个目录。但是,由于使用 transferTo(),我没有发现任何显着的性能改进。请让我知道文件到文件 sendFile 是否真的在 Linux 内核中有效,或者只有文件到套接字才有效?我需要为 sendFile 启用任何东西吗?

示例代码:

  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
            }*/
            cis.transferTo(0, len, cos);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } 
}
4

0 回答 0