Compare the performance of new and old I/O in Java--to replicate large files as an example

Source: Internet
Author: User

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

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.