Java I/O Stream

Source: Internet
Author: User

Java I/O Stream


I/O learning has been scattered all over the world. Today, I/O is specially designed to read the tutorials and take notes.

IO streamIt is used to process data transmission between devices. Java programs use stream to input and output data. The java. io package provides various "stream" classes and interfaces to obtain different types of data and input and output data using standard methods.

API structure of data stream



Stream classification




IO stream system


> Byte stream and byte stream
Byte stream <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/J0tS0psDty/nt0md1_m3k/examples + 3aOstKbA7bS/examples/vA4NDNyv2 + examples/fTw9PazsS8/examples + examples = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160409/04332S603-4.png" title =" \ "/>



> Four abstract base classes and common methods


> File stream (node stream)
"-FileInputStream |-FileOutPutStream
|-FileReadr |-FileWriter

Sample Code

package com.afy.iodemo;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileInputOutputStreamDemo { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("a.gif"); FileOutputStream fos = new FileOutputStream("b.gif"); byte input[] = new byte[50]; while (fis.read(input)!=-1) { fos.write(input); } fis.close(); fos.close(); System.out.println("done"); }}
package com.afy.iodemo;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class FileReaderWriterDemo { public static void main(String[] args) { FileReader fr = null; FileWriter fw = null; char c[] = null; int len; try { File src = new File("yoyo.txt"); File dest = new File("yoyo1.txt"); fr = new FileReader(src); fw = new FileWriter(dest); c = new char[40]; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { while ((len=fr.read(c))!=-1) { fw.write(c,0, len); } fw.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } }}


> Buffer stream
|-BufferedInputStream |-BufferedOutputStream
|-BufferedReader |-BufferedWriter

It is a type of stream processing that can improve read/write efficiency.

Sample Code:

Package com. afy. iodemo; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; public class BufferedInputOutputDemo {public static void main (String [] args) {try {FileInputStream FD = new FileInputStream ("reread. wmv "); BufferedInputStream bis = new Buffere DInputStream (fisms, 1000000); FileOutputStream fos = new FileOutputStream ("newmovie. wmv "); BufferedOutputStream bos = new BufferedOutputStream (fos, 1000000); byte input [] = new byte [100000]; int count = 0; long before = System. currentTimeMillis (); while (bis. read (input )! =-1) {count ++; bos. write (input);} System. out. println (count + "times"); System. out. println ("read time:" + (System. currentTimeMillis ()-before) + "ms"); bis. close (); FCM. close (); bos. close (); fos. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}

The BufferedReader and BufferedWriter examples are similar to the previous example, where BufferedReader providesReadLine () methodTo read a String. Although BufferedWriter provides the write (String sr) method, it cannot write a single String and can only use the print stream.Println (String x) method in PrintWriterWrite a line of string. Note that when writing data using a buffer stream, when the written data is not filled with the buffer, the content in the buffer is sometimes not written into the file, this will cause part of the file to be lost when writing the file. The solution is to call the corresponding stream after the write operation is completed.Fluch () methodForcibly output all content in the current buffer zone.


> Conversion stream

|-InputStreamReader transfers byte streams
|-OutputStreamWriter

First, they are bytes streams. Then, the read () method of InputStreamReader does not provide a method to read a string. It is very troublesome to directly read one or more strings. The solution is BufferedReader'sReadLine () methodRead a row at a time. When the row is marked, the character data before the row is returned as a string. When the end is read, null is returned. However, when writing a String using the write (String, str) in BufferedWriter, The newline String is not written. Similarly, although the write () method of OutputStreamWriter contains the write (String str) method, however, a line of strings cannot be written, so the text cannot be written with line breaks. Solution: In the PrintWriter class (print Stream)Println (String x) Method,

> Difficulties and obfuscation

|-> ReadLine () method in BufferedReader

|-> Println (String x) <method in PrintWriter

|-> Flush () method of the corresponding stream added after writing the file

> Legacy problems

|-Why does the read () method of byte stream return the int type instead of the byte type when reading a byte?

| The-write () method writes one byte at a time. It receives an int value, but only keeps the original byte and adds 0 to the remaining binary digits?

^_^ Are you making this a ghost?? Please tell me, thx!

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.