tags:

views:

100

answers:

3

Hi,

What ist the fastest way to copy a big number of files in Java. So far I have used file streams and nio. Overall streams seem to be faster than nio. What experiences did you make so far?

+1  A: 

Have java fork off an OS batch script that copies the files. Your code might have to write the batch script.

Tony Ennis
Already thought about that but the error handling would be to hard when copying 10,000+ files and the overhead of spawning a system thread when copying small files is to big. Furthermore the app wouldn't be platform independent.
Mato
1. You're going to have to handle error checking anyway. 2. You're correct in that it would not be platform independent - so you intend to run this on servers of different types? 3. Can you create the 10,000 files in the 'proper' place to begin with and not need a copy at all? 4. Don't spawn one thread per file. One thread per 100 files or something.
Tony Ennis
I agree with this. Dumb copying of files is not an ideal use case of Java. If you want to do it from Java, then fork off an OS level call.
bwawok
+2  A: 

I would use:

import java.io.*;
import java.nio.channels.*;

public class FileUtils{
    public static void copyFile(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);
        } 
        catch (IOException e) {
            throw e;
        }
        finally {
            if (inChannel != null) inChannel.close();
            if (outChannel != null) outChannel.close();
        }
    }

    public static void main(String args[]) throws IOException{
        FileUtils.copyFile(new File(args[0]),new File(args[1]));
  }
}

If any of your files are bigger than 64M in Windows you might need to look at this: http://forums.sun.com/thread.jspa?threadID=439695&messageID=2917510

Romain Hippeau
catch (IOException e) { throw e; } <--- nice one
unbeli
It's just sample code.
Tony Ennis
+2  A: 

http://www.baptiste-wicht.com/2010/08/file-copy-in-java-benchmark/ might get you your answer.

mihn
thanks, this answers my question
Mato