It is more convenient to use filechannel
/**
Copy files, using two techniques, FileChannels and streams.
Using FileChannels is usually faster than using streams.
*/
Public final class CopyFiles {
/* Change these settings before running this class .*/
/** The file to be copied .*/
Public static final String INPUT_FILE = "C: \ TEMP \ cottage.jpg ";
/**
The name of the copy to be created by this class.
If this file doesn't exist, it will be created, along with any
Needed parent directories.
*/
Public static final String COPY_FILE_TO = "C: \ TEMP10 \ cottage_2.jpg ";
/** Run the example .*/
Public static void main (String... runtime GS) throws IOException {
File source = new File (INPUT_FILE );
File target = new File (COPY_FILE_TO );
CopyFiles test = new CopyFiles ();
Test. copyWithChannels (source, target, false );
// Test. copyWithStreams (source, target, false );
Log ("Done .");
}
/** This may fail for VERY large files .*/
Private void copyWithChannels (File aSourceFile, File aTargetFile, boolean aAppend ){
Log ("Copying files with channels .");
EnsureTargetDirectoryExists (aTargetFile. getParentFile ());
FileChannel inChannel = null;
FileChannel outChannel = null;
FileInputStream inStream = null;
FileOutputStream outStream = null;
Try {
Try {
InStream = new FileInputStream (aSourceFile );
InChannel = inStream. getChannel ();
OutStream = new FileOutputStream (aTargetFile, aAppend );
OutChannel = outStream. getChannel ();
Long bytesTransferred = 0;
// Defensive loop-there's usually only a single iteration:
While (bytesTransferred <inChannel. size ()){
BytesTransferred + = inChannel. transferTo (0, inChannel. size (), outChannel );
}
}
Finally {
// Being defensive about closing all channels and streams
If (inChannel! = Null) inChannel. close ();
If (outChannel! = Null) outChannel. close ();
If (inStream! = Null) inStream. close ();
If (outStream! = Null) outStream. close ();
}
}
Catch (FileNotFoundException ex ){
Log ("File not found:" + ex );
}
Catch (IOException ex ){
Log (ex );
}
}
Private void copyWithStreams (File aSourceFile, File aTargetFile, boolean aAppend ){
Log ("Copying files with streams .");
EnsureTargetDirectoryExists (aTargetFile. getParentFile ());
InputStream inStream = null;
OutputStream outStream = null;
Try {
Try {
Byte [] bucket = new byte [32*1024];
InStream = new BufferedInputStream (new FileInputStream (aSourceFile ));
OutStream = new BufferedOutputStream (new FileOutputStream (aTargetFile, aAppend ));
Int bytesRead = 0;
While (bytesRead! =-1 ){
BytesRead = inStream. read (bucket); //-1, 0, or more
If (bytesRead> 0 ){
OutStream. write (bucket, 0, bytesRead );
}
}
}
Finally {
If (inStream! = Null) inStream. close ();
If (outStream! = Null) outStream. close ();
}
}
Catch (FileNotFoundException ex ){
Log ("File not found:" + ex );
}
Catch (IOException ex ){
Log (ex );
}
}
Private void ensureTargetDirectoryExists (File aTargetDir ){
If (! ATargetDir. exists ()){
ATargetDir. mkdirs ();
}
}
Private static void log (Object aThing ){
System. out. println (String. valueOf (aThing ));
}
}
This article is from "Qiao Lei's blog learning and progress"