18th Java I/O stream (3)

Source: Internet
Author: User

1. Input and output stream (single byte read)

 Packagecn.itcast_02;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;/**Requirement: Copy text content to another place (single byte copy)*/ Public classCopyfiledemo { Public Static voidMain (string[] args)throwsIOException {//Get Stream ObjectFileInputStream FIS =NewFileInputStream ("A.txt"); FileOutputStream Fos=NewFileOutputStream ("B.txt"); //first read the text content (input to write the data into the flow with the stream) and then output to another place//A byte -by-byte read (returned by the byte itself) reads a write a        inti = 0; //The return value I represents the byte itself         while((I=fis.read ())!=-1){            //WriteFos.write (i);        } fis.close ();    Fos.close (); }}

2. Input and output stream (byte array read)

1  Packagecn.itcast_02;2 ImportJava.io.FileInputStream;3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 /**6 Requirement: Copy text content to another place (byte array copy)7 */8  Public classCopyFileDemo2 {9      Public Static voidMain (string[] args)throwsIOException {Ten         //gets the stream object to copy A.txt to B.txt OneFileInputStream FIS =NewFileInputStream ("A.txt"); AFileOutputStream fos =NewFileOutputStream ("B.txt"); -         //defines an array of bytes -         byte[] bs =New byte[1024]; the         intlen = 0;//The read method with parameters returns the number of bytes fetched -          while((Len=fis.read (BS))!=-1){ - System.out.println (len); -Fos.write (bs,0, Len); +         } - fis.close (); + fos.close (); A     } at}

3. Buffer Stream

1  Packagecn.itcast_02;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.FileInputStream;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 9 /**Ten * Stream .....  One  */ A  Public classDemo { -      Public Static voidMain (string[] args)throwsIOException { -         LongStart =System.currenttimemillis (); the         //method1 ("Aaa.mp3", "Bbb.mp3");//556 Ms -         //method2 ("Aaa.mp3", "Bbb.mp3");//197 Ms -          //method3 ("Aaa.mp3", "Bbb.mp3");//... Milliseconds -METHOD4 ("Aaa.mp3", "Bbb.mp3");//1454 Ms +         LongEnd =System.currenttimemillis (); -SYSTEM.OUT.PRINTLN (End-start); +     } A  at     //Efficient buffered stream single byte read -      Public Static voidMethod1 (string s1, string s2)throwsIOException { -         //Get Stream Object -Bufferedinputstream bis =NewBufferedinputstream (NewFileInputStream (S1)); -Bufferedoutputstream BOS =NewBufferedoutputstream (Newfileoutputstream (S2)); -         //single byte read and write in         intI =0; -          while((I=bis.read ())!=-1){ to Bos.write (i); +         } - bis.close (); the bos.close (); *     } $ Panax Notoginseng     //efficient buffer stream byte array read -      Public Static voidMETHOD2 (string s1, string s2)throwsIOException { the         //Get Stream Object +Bufferedinputstream bis =NewBufferedinputstream (NewFileInputStream (S1)); ABufferedoutputstream BOS =NewBufferedoutputstream (Newfileoutputstream (S2)); the         //Cyclic read operations +         byte[] by =New byte[1024]; -         intLen = 0; $          while(len = Bis.read (by))! =-1) { $Bos.write (by, 0, Len); -         } - bis.close (); the bos.close (); -     }Wuyi  the     //normal stream single byte read -      Public Static voidMethod3 (string s1, string s2)throwsIOException { WuFileInputStream FIS =NewFileInputStream (S1); -FileOutputStream fos =Newfileoutputstream (S2); About         //single byte read and write $         inti = 0; -          while((I=fis.read ())!=-1){ - Fos.write (i); -         } A fis.close (); + fos.close (); the     } -  $     //normal stream byte array reads the      Public Static voidMETHOD4 (string s1, string s2)throwsIOException { theFileInputStream FIS =NewFileInputStream (S1); theFileOutputStream fos =Newfileoutputstream (S2); the         //read and write byte arrays -         byte[] by =New byte[1024]; in         intLen = 0; the          while(Len=fis.read (by))!=-1){ theFos.write (by,0, Len); About         } the fis.close (); the fos.close (); the     } +  -}

18th Java I/O stream (3)

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.