Java IO two major systems

Source: Internet
Author: User

Difference

One is the Input/output stream system, one is the reader/writer system, the difference between the two is

Stream is responsible for the data of the byte stream, Reader/writer is responsible for the character stream.


Design mode decorator Mode

This mode is mainly used to continuously increase the subordinate function without changing the existing component design and code. Make a metaphor, like you have a picture, with one or more different frames.

For details, refer to here: http://www.cnblogs.com/bastard/archive/2012/02/02/2336150.html

Componnet to support the basic interface of decorator, which is what must be done at design time.


Both sets of IO systems are typical decorator models. The following article is an introduction to the relatively good.

http://my.oschina.net/gao0516/blog/136103


Adapter mode

This pattern is used to convert an object to an interface that can be used by another object. There are implementations within both systems. At the same time, the two modes of interconnection also rely on this model. There are two main classes:

InputStreamReader and OutputStreamWriter

From the name can be seen, both stream and reader/writer, a typical cross-system adapter.


For example, if you want to write a file with writer, the code is as follows:

    static private void Test2 () {        try {            printwriter printwriter = new PrintWriter ("/home/dean/test2.log");            Printwriter.println ("Hello, China");            Printwriter.flush ();            Printwriter.close ();        } catch (IOException e) {            System.out.println (e.getmessage ());        }    }

After the run, the Test2.log file was created and there appeared a Hello, China.


However, there is a problem that the PrintWriter constructor does not accept the APPEND option, so each time the file is purged and re-written, the append write cannot be supported. and the stream system supports it. Therefore, the adapter model is used to mix the two systems.

    static private void Test1 () {        try {            Writer w = new OutputStreamWriter (New FileOutputStream ("/home/dean/test1.log ", true)," UTF-8 ");            PrintWriter printwriter = new PrintWriter (w);            Printwriter.println ("Hello, China");            Printwriter.flush ();            Printwriter.close ();        } catch (IOException e) {            System.out.println (e.getmessage ());        }    }

Using the FileOutputStream support append features, with OutputStreamWriter do adapter, now can.


So it looks like the Reader/writer system features focus on text character processing, while the advanced functionality of some streaming operations relies on the mate of the stream system.








Java IO two major systems

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.