Java Fundamentals Enhanced IO flow Note 44:io 4 ways to copy pictures from a stream exercise case

Source: Internet
Author: User

1.4 ways to copy a picture case:

Analysis:

Copy the data, if we know to open with Notepad and can read, use a character stream, otherwise with the word stream.

By this principle, we know that we should adopt a byte stream.

There are 4 ways to do this, so we have 4 ways of doing this. Recommended to master the 4th kind.

Data Source :

C:\\a.jpg--FileInputStream--Bufferedinputstream

Destination :

D:\\b.jpg--FileOutputStream--Bufferedoutputstream

2. 4 ways code Examples:

1  Packagecn.itcast_01;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.File;6 ImportJava.io.FileInputStream;7 ImportJava.io.FileOutputStream;8 Importjava.io.IOException;9 Ten /* One * Copy Pictures A  *  - * Analysis: - * Copy data, if we know to open with Notepad and can read, use a character stream, otherwise with the word stream.  the * Through this principle, we know that we should adopt a byte stream.  - * And there are 4 ways of byte stream, so we have 4 ways to do this topic. recommended to master the 4th kind .  -  *  - * Data source : + * c:\\a.jpg --fileinputstream--Bufferedinputstream - * destination : + * d:\\b.jpg --fileoutputstream--Bufferedoutputstream A  */ at  Public classCopyimagedemo { -      Public Static voidMain (string[] args)throwsIOException { -         //using a string as a path -         //String srcstring = "c:\\a.jpg"; -         //String deststring = "d:\\b.jpg"; -         //using the file object as a parameter inFile Srcfile =NewFile ("C:\\a.jpg"); -File DestFile =NewFile ("D:\\b.jpg"); to  +         //method1 (Srcfile, destfile); -         //method2 (Srcfile, destfile); the         //method3 (Srcfile, destfile); * method4 (Srcfile, destfile); $     }Panax Notoginseng  -     // byte buffer stream reads and writes one byte array at a time the     Private Static voidMETHOD4 (file srcfile, file destfile)throwsIOException { +Bufferedinputstream bis =NewBufferedinputstream (NewFileInputStream ( A srcfile)); theBufferedoutputstream BOS =NewBufferedoutputstream ( +                 NewFileOutputStream (DestFile)); -  $         byte[] Bys =New byte[1024]; $         intLen = 0; -          while(len = Bis.read (bys))! =-1) { -Bos.write (bys, 0, Len); the         } - Wuyi bos.close (); the bis.close (); -     } Wu  -     // byte buffer stream reads and writes one byte at a time About     Private Static voidMethod3 (file srcfile, file destfile)throwsIOException { $Bufferedinputstream bis =NewBufferedinputstream (NewFileInputStream ( - srcfile)); -Bufferedoutputstream BOS =NewBufferedoutputstream ( -                 NewFileOutputStream (DestFile)); A  +         intby = 0; the          while(by = Bis.read ())! =-1) { - Bos.write (by); $         } the  the bos.close (); the bis.close (); the     } -  in     // Basic byte stream reads and writes one byte array at a time the     Private Static voidMETHOD2 (file srcfile, file destfile)throwsIOException { theFileInputStream FIS =NewFileInputStream (srcfile); AboutFileOutputStream fos =NewFileOutputStream (destfile); the  the         byte[] Bys =New byte[1024]; the         intLen = 0; +          while(len = Fis.read (bys))! =-1) { -Fos.write (bys, 0, Len); the         }Bayi  the fos.close (); the fis.close (); -     } -  the     // Basic byte stream reads and writes one byte at a time the     Private Static voidMethod1 (file srcfile, file destfile)throwsIOException { theFileInputStream FIS =NewFileInputStream (srcfile); theFileOutputStream fos =NewFileOutputStream (destfile); -  the         intby = 0; the          while(by = Fis.read ())! =-1) { the Fos.write (by);94         } the  the fos.close (); the fis.close ();98     } About}

Java Fundamentals Enhanced IO flow Note 44:io 4 ways to copy pictures from a stream exercise case

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.