Use FileChannel to copy files

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.