Java Copy Picture exercise

Source: Internet
Author: User

/* Copy the picture. In fact, the copy media file is basically a byte stream file. Copying music is similar to copying a picture. The picture is a media file, so you use InputStream and outputstream for the copy operation. */import java.io.*;class copypictest {public static void main (string[] args) {long start=system.currenttimemillis (); Copypic (); Long Mid=system.currenttimemillis (); Copypicarr (); Long End=system.currenttimemillis (); System.out.println ("1:" + (Mid-start) + "MS"); System.out.println ("2:" + (End-mid) + "MS");} /* Do not use array as buffer */public static void Copypic () {FileInputStream fis=null; FileOutputStream Fos=null; FileWriter fw=null;try{fis=new fileinputstream ("d:\\ picture \\20.jpg"); Fos=new fileoutputstream ("d:\\ picture \\21.jpg"); fw= New FileWriter ("d:\\ picture \\pic.txt"); int By;while ((By=fis.read ())!=-1) {fos.write ((char) by); Fw.write (by);}} catch (IOException e) {throw new RuntimeException ("Copy Failed");} Finally{try{if (Fis!=null) Fis.close ();} catch (IOException e) {throw new RuntimeException ("read stream object shutdown failed");} Try{if (Fos!=null) Fos.close ();} catch (IOException e) {throw new RuntimeException ("write stream object shutdown failed");}}} /**/public static void Copypicarr () {FileInputStream Fis=nulL FileOutputStream fos=null;try{fis=new fileinputstream ("d:\\ picture \\20.jpg"); Fos=new fileoutputstream ("d:\\ picture \\22.jpg" ) int len;byte[] B=new byte[1024];while ((Len=fis.read (b))!=-1) {fos.write (B,0,len);}} catch (IOException e) {throw new RuntimeException ("Copy Failed");} Finally{try{if (Fis!=null) Fis.close ();} catch (IOException e) {throw new RuntimeException ("read stream object shutdown failed");} Try{if (Fos!=null) Fos.close ();} catch (IOException e) {throw new RuntimeException ("write stream object shutdown failed");}}}

Java Copy Picture exercise

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.