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