Black Horse programmer--22, byte stream inputstream,outputstream, byte throttle buffer technology, Bufferedinputstream,bufferedoutputstream

Source: Internet
Author: User

------<ahref= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Black Horse programmer--22, byte stream inputstream,outputstream, byte throttle buffer technology, Bufferedinputstream,bufferedoutputstream

/*

The relevant explanation of the byte stream:

InputStream (read operation), OutputStream (write operation)

Similar to the previous character stream in usage

*/

Import java.io.*;class ioliou13{public static void Main (string[] args) throws IOException {//              Method ();                               Method1 (); } public static void Method () throws IOException {FileInputStream fis=new fileinputstre       AM ("f:\\yuyuyu.txt");       int i=0;                   Char ch= ' 0 ';                        while ((I=fis.read ())!=-1) {ch= (char) i;                             SOC (CH);                    /* Since this is a byte-stream object operation, only numbers and letters can be seen here at this time */         } fis.close (); } public static void Method1 () throws IOException {FileInputStream fis=new fileinputstr                      EAM ("F:\\yuyuyu.txt");                      int num=fis.available ();//Get the number of bytes Soc ("num=" +num); Byte[] B=new Byte[3];                      int i=0;                      char ch = ';                                  while ((I=fis.read (b))!=-1) {//ch= (char) i;                               Soc (new String (b,0,b.length));         This is to print out characters according to byte array B, so there is no need to force type conversion} fis.close (); } public static void Method2 () throws IOException {FileOutputStream fos=new fileoutputstr                     EAM ("F:\\yuyuyu.txt");                   Fos.write ("Kankan". GetBytes ());                            The GetBytes method is a string class that uses its own method to convert bytes/* bytes to the write stream without the Flush method because the byte is already the smallest operand.                  The byte write stream is written directly to the file.         */Fos.close ();                                   } public static void Soc (Object obj) {System.out.print (obj); }}

—————— Split Line ——————

/*

To copy a picture:

Select byte stream

Ideas:

1, byte read stream associated with target

2, byte write stream create picture file

3, cyclic reading and writing

4, close the resource

*/

Import java.io.*;class ioliou14{public static void Main (string[] args) {method2 ();             } public static void method () {FileInputStream fis=null;                   FileOutputStream Fos=null;                     try {fis=new FileInputStream ("f:\\ untitled 23.png");                   Fos=new FileOutputStream ("f:\\ untitled 23_copy.png");                   Byte[] B=new byte[1024];                   int i=0;                   while ((I=fis.read (b))!=-1) {fos.write (b,0,i);                            }} catch (IOException e) {                   throw new RuntimeException ("Problem copying picture");                                     } finally {try {                            Fis.close ();  }                          catch (IOException e) {throw n                             EW RuntimeException ("Fis.close (); a problem");                        } try {fos.close (); } catch (IOException E2) {throw new Runtime                                        Exception ("Fos.close (); a problem"); }}} Publ                   IC static void Method2 () {FileInputStream fis=null;                   FileOutputStream Fos=null;              try {fis=new FileInputStream ("f:\\ transcoding video \ Dead Zone 01.mp4");              Fos=new FileOutputStream ("f:\\ Death Zone 01_copy.mp4");              Byte[] B=new byte[1024];              int i=0; While((I=fis.read (b))!=-1)                                 {Fos.write (b,0,i);                            }} catch (IOException e) {                    throw new RuntimeException ("Replication Problem");                       } finally {try {fis.close (); } catch (IOException e) {T                      Hrow New RuntimeException ("Fis.close (); the problem");                } try {fos.close (); } catch (IOException E2) {throw new RuntimeException ("Fos.                 Close (); problem "); }}} public static void Soc (Object obj) {System.out.printl                   n (obj);     }} 

—————— Split Line ——————

/*

The byte stream also has its own buffer technology:

Bufferedinputstream

Bufferedoutputstream

The usage is almost the same as the character stream.

*/

Import java.io.*;class ioliou15{public static void Main (string[] args) throws IOException {                     Long Start=system.currenttimemillis ();                      Method ();                    Long End=system.currenttimemillis ();                            Soc ("time=" + (End-start)); } public static void Method () throws IOException {FileInputStream fis=new Fileinpu                   TStream ("f:\\ untitled 23.png");                   FileOutputStream fos=new FileOutputStream ("f:\\ untitled 23copy.png");                   Bufferedinputstream bis=new bufferedinputstream (FIS);                   Bufferedoutputstream bos=new bufferedoutputstream (FOS);                   int i=0;                            while ((I=bis.read ())!=-1) {bos.write (i);                  Here directly throw I in can, more convenient} bis.close ();                              Bos.close ();      } public static void Soc (Object obj) {System.out.println (obj); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Black Horse programmer--22, byte stream inputstream,outputstream, byte throttle buffer technology, Bufferedinputstream,bufferedoutputstream

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.