/* 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