Re: Copy files in java gives me out of memory error



The best and fastest way to copy a file is as follows:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

....

// Get an input channel
final FileChannel ich = new FileInputStream(input).getChannel();

// Get an output channel
final FileChannel och = new FileOutputStream(tofile).getChannel();

// Get the size of the input
final long size = ich.size();

// Begin at the beginning
long position = 0;

// Copy chunks of the file until finished
while (position < size)
position += och.transferFrom(ich, position, size - position);

// Close the files
ich.close();
och.close();

--
John W. Kennedy
"Sweet, was Christ crucified to create this chat?"
-- Charles Williams. "Judgement at Chelmsford"
.