The fastest way to copy java files!

Source: Internet
Author: User
Package a_a_a;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.nio.ByteBuffer;

Import Java.nio.channels.FileChannel;
		public class FileCopy {public static void main (string[] args) {File Srcfile = new File ("g:/2144/bei.xlsx");
		File Destdir = new file ("g:/2144/bei/");
		String newfilename = "bei.xlsx";			
		
		
Filecopy.copyfile (Srcfile, Destdir, NewFileName);
Long T1 = System.currenttimemillis ();			
for (int i = 0; i < i++) {//Filecopytest.copyfile1 (Srcfile, Destdir, NewFileName);
		Long t2 = System.currenttimemillis ();			
		for (int i = 0; i < i++) {Filecopy.copyfile (Srcfile, Destdir, NewFileName);
long t3 = System.currenttimemillis ();
		System.out.println (T2-T1);
		System.out.println (T3-T2);
	SYSTEM.OUT.PRINTLN (250);
	 /** * Copy Files* * @param srcfile * source file * @param destdir * target directory file * @param newfilename * New filename * @return The actual number of bytes copied, if the file, directory does not exist, the file is null, or an IO exception occurs, return-1/public long copyFile1 (file srcfile, file Destdir, Stri
		Ng NewFileName) {long copysizes = 0;
			if (!srcfile.exists ()) {System.out.println ("source file does not exist");
		copysizes =-1;
			else if (!destdir.exists ()) {System.out.println ("Destination directory does not exist");
		copysizes =-1;
			else if (NewFileName = = null) {System.out.println ("file name is null");
		copysizes =-1;
				else {try {Bufferedinputstream bin = new Bufferedinputstream (new FileInputStream (srcfile));
				Bufferedoutputstream bout = new Bufferedoutputstream (new FileOutputStream (New File (Destdir, NewFileName));
				int b = 0, i = 0;
					while ((b = bin.read ())!=-1) {bout.write (b);
				i++;
				} bout.flush ();
				Bin.close ();
				Bout.close ();

			copysizes = i; The catch (FileNotFoundException e) {e.printstacktrace ();
			catch (IOException e) {e.printstacktrace ();
	} return copysizes;  /** * Copy files (copy files at very fast speed) * * @param srcfile * source file * @param destdir * Target directory file * @param newfilename * New file name * @return The actual number of bytes copied, if the file, directory does not exist, the file is null, or an IO exception occurs, return-1/public static long copy
		File (file srcfile, file Destdir, String newfilename) {long copysizes = 0;
			if (!srcfile.exists ()) {System.out.println ("source file does not exist");
		copysizes =-1;
			else if (!destdir.exists ()) {System.out.println ("Destination directory does not exist");
		copysizes =-1;
			else if (NewFileName = = null) {System.out.println ("file name is null");
		copysizes =-1;
				else {try {filechannel fcin = new FileInputStream (srcfile). Getchannel ();
				FileChannel fcout = new FileOutputStream (new File (Destdir, NewFileName)). Getchannel ();
				Long size = Fcin.size ();
				Fcin.transferto (0, Fcin.size (), fcout);
				Fcin.close ();
				Fcout.close ();
			copysizes = size; catch (FILENOTFOUndexception e) {e.printstacktrace ();
			catch (IOException e) {e.printstacktrace ();
	} return copysizes;
 }
}


 

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.