Java I/O applications of several important streams, java

Source: Internet
Author: User

Java I/O applications of several important streams, java

IO Flowchart

Master 13 Io streams

 

1. FileInputStream and FileoutputStream

FileInputStream: File Reading

1 public class readers 2 {3 public static void main (String [] args) throws Exception 4 {5 File file = new File ("F: \ java \ workspace \ Fanshe \ src \ com \ cyg \ fanshe. java "); // read file 6 FileInputStream fi = new FileInputStream (file); // create a byte stream to open the file 7 byte [] B = new byte [fi. available ()]; // fi. available can be used to obtain the number of bytes occupied by the file. 8 int a =-1; 9 while (a = fi. read (B ))! =-1) // determine whether the file has reached the end of the file 10 {11 // System. out. println (new String (B); 12} 13 System. out. println (new String (B); 14 // disable stream 15 fi. close (); 16 17} 18}

FileoutputStream: file writing

1 public class output 2 {3 public static void main (String [] args) throws Exception 4 {5 File file = new File ("F: \ a.txt "); 6 FileOutputStream out = new FileOutputStream (file); 7 out. write ("abmin ". getBytes (); 8 out. flush (); // clear cache 9 out. close (); // close stream 10} 11}

 

Ii. BufferedInputStream and BufferedOutputStream

BufferedInputStream is a buffer stream.

Function: adds some functions for another input stream, namely buffering input and supportingmark(TAG) andresetThe reset method is implemented through an internal buffer array.

 

 1 public class buffertream 2 { 3     public static void main(String[] args) throws Exception 4     { 5         FileInputStream in = new FileInputStream("E:\\win10 64\\vmware.log"); 6         BufferedInputStream bu = new BufferedInputStream(in); 7         FileOutputStream out = new FileOutputStream("E:\\a.txt"); 8         BufferedOutputStream bo = new BufferedOutputStream(out); 9         10         byte[] b = new byte[1024];11         int a = bu.read(b);12         while(a != -1)13         {14             //System.out.println(Arrays.toString(b));15             bo.write(b, 0, a);16             a = bu.read(b);17         }18         in.close();19         bu.close();20         21         bo.flush();22         out.close();23         bo.close();24     }25 }

The running result is as follows:

 

3. DataOutputStream and DataInputStream

It is a data input stream that inherits FilterInputStream and is used to decorate other input streams.

1 public class adtastream 2 {3 public static void main (String [] args) throws Exception 4 {5 FileOutputStream out = new FileOutputStream ("E: \ B .txt "); 6 DataOutputStream dot = new DataOutputStream (out); // The filter stream must have a node stream 7 8 // write data 9 dot. writeInt (10); 10 dot. writeBytes ("admin"); 11 dot. writeChar ('A'); 12 dot. writeBoolean (true); 13 14 out. flush (); 15 out. close (); 16 dot. close (); 17 18 FileInputStream in = new FileInputStream ("E: \ B .txt"); 19 DataInputStream di = new DataInputStream (in ); 20 21 // when reading data, it must be the same as the write order. Otherwise, the structure 22 System will be damaged. out. println (di. readInt (); 23 System. out. println (di. readByte (); 24 System. out. println (di. readChar (); 25 System. out. println (di. readBoolean (); 26} 27}

 

 

4. ByteArrayInputStream and ByteArrayOutputStream

ByteArrayInputStream is used to read data in memory.

1 public class bytestream 2 {3 public static void main (String [] args) throws Exception 4 {5 ByteArrayOutputStream out = new ByteArrayOutputStream (); 6 String str = "admin "; 7 out. write (str. getBytes (); 8 byte [] B = new byte [200]; 9 ByteArrayInputStream in = new ByteArrayInputStream (str. getBytes (); // ByteArrayInputStream: stream 10 in. read (B); 11 System. out. println (Arrays. toString (B); 12} 13}

The bottom source code of the system corresponding to damin is printed.

 

5. ObjectInputStream and ObjectoutputStream

 

6. BufferedReader and BufferedWriter

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.