Java fourth time job

Source: Internet
Author: User

This program is optimized to read large files (test file 14M), written as two static methods, convenient to call, the code is as follows:

ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classTest { Public Static voidTestfile (file Filein, file fileout)throwsIOException {//determine if a file exists        if(!filein.exists ()) {System.out.println (Filein.getname ()+ "does not exist"); Throw NewIOException (); }        //If the file does not exist, create        if(!fileout.exists ())            {Fileout.createnewfile (); Throw NewIOException (); } FileInputStream in=NewFileInputStream (Filein); FileOutputStream out=NewFileOutputStream (fileout); intb; //Copy         while((b = In.read ())! =-1) {out.write (b);        } in.close ();    Out.close (); }         Public Static voidTestFileQuick1 (file Filein, file fileout)throwsIOException {//determine if a file exists        if(!filein.exists ()) {System.out.println (Filein.getname ()+ "does not exist"); Throw NewIOException (); }        //If the file does not exist, create        if(!fileout.exists ())            {Fileout.createnewfile (); Throw NewIOException (); } FileInputStream in=NewFileInputStream (Filein); FileOutputStream out=NewFileOutputStream (fileout); byte[] buf =New byte[20 * 1024]; intbytes = 0; //Copy         while((bytes = In.read (buf, 0, buf.length))! =-1) {out.write (buf,0, bytes);        } in.close ();    Out.close (); }     Public Static voidMain (string[] args) {Try {            LongStart =System.currenttimemillis (); Test.testfile (NewFile ("C:\\users\\cumin\\desktop\\1.mp3"),                    NewFile ("C:\\users\\cumin\\desktop\\6.mp3")); LongEnd =System.currenttimemillis (); System.out.println (End-start); } Catch(IOException e) {e.printstacktrace (); }    }}

By using the static method Testfile read-by-byte replication requires:

Test.testfilequick1 (new file ("C:\\users\\cumin\\desktop\\1.mp3"),                    new file ("c:\\ Users\\cumin\\desktop\\6.mp3 "));             long end = system.currenttimemillis ();             -start);

After replacing the static method TestFileQuick1:

PS: Read mainly throughread(byte[] b, int off, int len) 提升速度。

Write mainly throughwrite(byte[] b, int off, int len) 提升速度。

Replication speed greatly increased ~ ~ and the file does not exist will be created automatically.

Java fourth time job

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.