Java IO systems and Java IO Systems

Source: Internet
Author: User

Java IO systems and Java IO Systems
Differences

One is the input/output stream system and the other is the reader/writer system. The difference between the two systems is:

Stream is responsible for byte stream data, and reader/writer is responsible for bytes stream.


Design Mode: decorator Mode

This mode is mainly used to add ancillary functions without changing the existing component design and code. Just as if you have a pair of paintings, you can configure one or more different frames for them.

Specific can refer to here: http://www.cnblogs.com/bastard/archive/2012/02/02/2336150.html

Componnet must support the basic interface of decorator, Which is required during design.


Both IO systems are typical decorator models. The following article is a good introduction.

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


Adapter Mode

This mode is used to convert an object into an interface that can be used by another object. These two systems are implemented internally. At the same time, the two models are also dependent on each other. There are two main classes:

InputStreamReader and OutputStreamWriter

It can be seen from the names that there are both stream and reader/writer, typical cross-system adapter.


For example, to write a file using 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 running, the test2.log file is created, which contains hello, China.


However, there is a problem. The PrintWriter constructor does not accept the append option. Therefore, each time the file is cleared and re-written, append write is not supported. Supported in the stream system. Therefore, the adapter mode 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 ());}}

The append feature of FileOutputStream is used and OutputStreamWriter is used as the adapter.


Therefore, it seems that the reader/writer system focuses on text character processing, while the advanced features of some stream operations still depend on the combination of stream 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.