Java input and output stream

Source: Internet
Author: User

This article is not intended to introduce the Java input and output stream mechanism, only for personal notes

As a Java side dish, every time the Internet search other people's Java read and write files of the program reference, always feel confused, why to declare so many classes, rules is what, completely divided からない, so with a little bit of doubt about it;

There are two classes of input and output modes in Java, byte-oriented (Inputstream&outputstream) and character-oriented (Reader&writer), and a class (file) for processing files and folders.

Java input and output stream class using decorator mode, from the above 5 classes derived from a lot of other classes, here to put a picture on the internet, we feel the next

Java supports many types of input sources and outputs, but it's not going to unfold, I just want to talk about the read and output of the file. It is well known that computers accessing external devices are time consuming and access buffers are fast. Therefore, in order to improve the efficiency of reading and writing files, the buffer mechanism needs to be added in addition to the necessary reading and writing mechanisms between program and flow nodes.

Specific examples are as follows:

 Packagecom.practise.test;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStreamReader;ImportJava.io.BufferedReader;ImportJava.io.OutputStreamWriter;ImportJava.io.BufferedWriter;//All classes ending with Stream are byte-oriented, such as Fileinputstream/fileoutputstream//classes ending in Reader/writer are character-oriented//classes beginning with File are oriented to external memory//classes that start with Buffered are buffer-oriented Public classReadAndWrite { Public Static voidMain (string[] args) {String path= "D:\\coding\\java\\testjava\\testdata\\"; Try{File InFile=NewFile (path + "In.txt")); File OutFile=NewFile (path + "OUT.txt")); InputStreamReader INSR=NewInputStreamReader (NewFileInputStream (inFile)); BufferedReader INBR=NewBufferedReader (INSR); OutputStreamWriter OUTSW=NewOutputStreamWriter (NewFileOutputStream (outFile)); BufferedWriter OUTBW=NewBufferedWriter (OUTSW); String Line= "";  while(line = Inbr.readline ())! =NULL) {Outbw.write ( line+ "\ r \ n");            } outbw.close ();            Outsw.close ();            Inbr.close ();                    Insr.close (); }        Catch(Exception e) {e.printstacktrace (); }            }}
View Code

Java input and output stream

Related Article

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.