Java I/O stream

Source: Internet
Author: User


Learning IO has been fragmented, today specially to see the tutorial, read the data to do notes to organize the I/O this piece

IO streams are used to process data transfer between devices. The Java program uses the "Flow" method for data input and output operation. A variety of "stream" classes and interfaces are available under the Java.io package to obtain different kinds of data and input and output data through standard methods.

The API structure of the data flow



Classification of streams




IO Flow System


> Byte stream and Character stream
byte stream read, read to a byte to return a byte, can handle all types of data, such as text, pictures, MP3, AVI, etc.;
The character stream can read to multiple bytes (the Chinese corresponding byte number is 2, the UTF-8 Code table is 3 bytes), first check the specified encoding table, will find the character return. Only character data can be processed, and processing of plain text data takes precedence over character streams, and other types of data with byte streams.

> Node streams (directly acting on files) and processing streams




> Four abstract base classes and common methods


> File Stream (Node stream)
|-fileinputstream |-fileoutputstream
|-filereadr |-filewriter

Sample code

 PackageCom.afy.iodemo;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.IOException; Public  class fileinputoutputstreamdemo {     Public Static void Main(string[] args)throwsIOException {FileInputStream FIS =NewFileInputStream ("A.gif"); FileOutputStream fos =NewFileOutputStream ("B.gif");byteInput[] =New byte[ -]; while(Fis.read (Input)!=-1) {fos.write (input);        } fis.close ();        Fos.close (); System.out.println ("Done"); }}
 PackageCom.afy.iodemo;ImportJava.io.File;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException; Public  class filereaderwriterdemo {     Public Static void Main(string[] args) {FileReader FR =NULL; FileWriter FW =NULL;CharC[] =NULL;intLenTry{File src =NewFile ("Yoyo.txt"); File dest =NewFile ("Yoyo1.txt"); FR =NewFileReader (SRC); FW =NewFileWriter (dest); c =New Char[ +]; }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 (); }    }}


> Buffered Streams
|-bufferedinputstream |-bufferedoutputstream
|-bufferedreader |-bufferedwriter

One of the processing streams that improves read and write efficiency

Example code:

 PackageCom.afy.iodemo;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.IOException; Public  class bufferedinputoutputdemo {     Public Static void Main(string[] args) {Try{FileInputStream FIS =NewFileInputStream ("Reread.wmv"); Bufferedinputstream bis =NewBufferedinputstream (FIS,1000000); FileOutputStream fos =NewFileOutputStream ("Newmovie.wmv"); Bufferedoutputstream BOS =NewBufferedoutputstream (FOS,1000000);byteInput[] =New byte[100000];intCount =0;LongBefore = 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 ();            Fis.close ();            Bos.close ();        Fos.close (); }Catch(FileNotFoundException e)        {E.printstacktrace (); }Catch(IOException e)        {E.printstacktrace (); }    }}

The BufferedReader and bufferedwriter examples are similar to the example above, where BufferedReader provides a ReadLine () method to read a line of strings, BufferedWriter provides write ( String sr) method, but cannot write a line of strings, only the println (String x) method in print stream PrintWriter is used to write out a line of strings. It is important to note that when the buffer stream is used to write data, when the data is not filled in the buffer, the contents of the buffer is sometimes not written in the file, it will cause the file to write the file part of the loss, the solution is to complete the write operation after the call to the corresponding stream Fluch () method Forces all the contents of the current buffer to be output.


> Conversion Flow

|-inputstreamreader byte flow character stream
|-outputstreamwriter character Flow byte stream

First they are character streams, and then the InputStreamReader read () method does not provide a way to read a string, and reading one or a line of strings directly can become cumbersome, and the solution is BufferedReader readLine () method , a row is read one at a time, and the character data before the row tag is returned as a string when the row is marked. When the end is read, NULL is returned. However, when writing a string using Write (STRING,STR) in BufferedWriter, it does not write a newline string, and similarly, although there is a write (string str) method in OutputStreamWriter's write () method , but it is not possible to write a line of strings so that the text you write cannot be wrapped. The solution is the println (String x) method in the PrintWriter class (the print stream),

> Easy to confuse points of difficulty

The ReadLine () method in |->bufferedreader

println (String X) < methods in |->printwriter

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

> Legacy Issues

|-the Read () method of the byte stream reads one bytes why is the int type instead of the byte type?

The |-write () method writes one byte at a time, receives a numeric value of type int, but retains only the original byte, and complements the remaining bits by 0?

^_^ You made this a god-horse ghost ? Please tell me,thx!

Java I/O stream

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.