Re: Copy files in java gives me out of memory error
- From: "John W. Kennedy" <jwkenne@xxxxxxxxxxxxx>
- Date: Wed, 26 Dec 2007 22:15:05 -0500
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"
.
- References:
- Copy files in java gives me out of memory error
- From: Canned
- Re: Copy files in java gives me out of memory error
- From: Daniel Pitts
- Copy files in java gives me out of memory error
- Prev by Date: Re: Copy files in java gives me out of memory error
- Next by Date: Re: Excellent oppurtunity for J2EE Professionals
- Previous by thread: Re: Copy files in java gives me out of memory error
- Next by thread: Re: Copy files in java gives me out of memory error
- Index(es):