Package Newio;import java.io.*;import Java.nio.bytebuffer;import java.nio.channels.filechannel;/* * use old and new i/respectively O Package API Copy file, compare performance */public class Copyfilecompara {public void Copyfileold (file From,file to) throws ioexception{//use traditional i/ o Stream processing (added buffer mechanism) FileInputStream fin=new FileInputStream (from); FileOutputStream fout=new FileOutputStream (to); Bufferedinputstream in=new Bufferedinputstream (Fin); Bufferedoutputstream out=new Bufferedoutputstream (fout); int B;long start=system.currenttimemillis (); while (b= In.read ())!=-1) {out.write (b);} Long cost= (System.currenttimemillis ()-start)/1000; System.out.println ("Old I/O (using buffering) time consuming:" +cost+ "s"); Out.close (); In.close ();} public void Copyfilenew (File from,file to) throws ioexception{//uses channel and buffer to process FileChannel fin=new FileInputStream ( From). Getchannel (); FileChannel fout=new FileOutputStream (To) Getchannel (); Long Start=system.currenttimemillis (); Bytebuffer Bfi=fin.map (FileChannel.MapMode.READ_ONLY, 0, From.length ()); Fout.write (BFI); Long cost= ( System.currenttimemillis ()-stART)/1000; System.out.println ("New I/O time:" +cost+ "s"); Fout.close (); Fin.close ();} public static void Main (string[] args) throws IOException {//Copy the file larger will better reflect the effect of the new Copyfilecompara (). Copyfileold (New file ("f:\\01 first speaking. wmv"), New file ("f:\\ copy 1.wmv"), New Copyfilecompara (). copyfilenew (New file ("f:\\01 first. wmv"), New file ( "f:\\ copy 2.wmv");}}
Compare the performance of new and old I/O in Java--to replicate large files as an example