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.