Filechannel.Transferto Для большого файла в Windows

Использование использования Java NIO может скопировать файл быстрее. Я нашел два вида метода в основном через Интернет, чтобы сделать эту работу.

public static void copyFile(File sourceFile, File destinationFile) throws IOException {
    if (!destinationFile.exists()) {
        destinationFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destinationFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

В 20 Очень полезные фрагменты кода Java для разработчиков Java Я нашел другой комментарий и трюк:

public static void fileCopy(File in, File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {
        // inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows
        // magic number for Windows, (64Mb - 32Kb)
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            position += inChannel.transferTo(position, maxCount, outChannel);
        }
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

, но я не нашел и не понять, что означает

«Магическое число для Windows , (64 МБ - 32 КБ) "

написано, что jackannel.transferto (0, jackannel.size (), нестандарный) имеет проблемы в Windows, составляет 32768 (= (64 * 1024 * 1024) - (32 * 1024)) Байт оптимален для этого метода.

8
задан Tapas Bose 11 September 2011 в 16:04
поделиться