FileChannel.transferTo pour les gros fichiers dans windows

À l'aide de Java NIO utiliser pouvez copier des fichiers plus vite. J'ai trouvé deux genre de méthode, principalement sur internet pour faire ce travail.

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();
        }
    }
}

Dans 20 très utile Java des extraits de code pour les Développeurs Java j'ai trouvé un commentaire différent et astuce:

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();
        }
    }
}

Mais je n'ai pas trouver ou à comprendre quel est le sens de

"nombre magique pour Windows, (64 mo - 32 ko)"

Il est dit que inChannel.transferTo(0, inChannel.size(), outChannel) a un problème dans windows, est 32768 (= (64 * 1024 * 1024) - (32 * 1024)) octet est optimale pour cette méthode.

OriginalL'auteur Tapas Bose | 2011-09-11