Java. Io package
Keywords: Java Io
Sender: hzxdark (dark wing), email area: Java
Question: Java. Io package detailed explanation, hope to help younger siblings ^_^
Mail station: Liyuan morning wind BBS station (Thu Dec 21 23:13:30 2006), within the station
I don't know what it was like when I was studying Java, but I felt confused when I first learned java. I/O packages. So in this article, we try to describe the structure of the Java. Io package as easily as possible. I hope the younger siblings who are confused about Java. Io will be somewhat helpful.
When I started learning Java, the introduction of Java. Io was read in Java programming ideas. To be honest, I couldn't understand it at the time -- "Java. Io is built in the 'destorator mode'." -- when I was just learning Java, I knew what a thing called decorator ......
But to understand java. Io, you must understand the decorator design pattern. The following is a detailed introduction.
The so-called decorator and decoration are actually a design technique. To put it bluntly, there is no difficulty, don't look at the hype of many online materials. (I hate some articles that always describe simple problems as complicated as the kiss problem of two pig ......).
The structure of the decorator is as follows:
Myinterface
|
_______ | _______
|
Myclass decorator
____ | _____
|
Decoratora decoratorb
The purpose of the decorator is to add new functions without changing any of the original classes (you can understand it as the flexibility mentioned in the book ). Here, myclass is the class you want to expand. decoratora and decoratorb encapsulate the functions you want to expand and save a reference to myinterface.
Consider the following code:
Public static void main (strings [] Arg ){
Myinterface A = new myclass ();
A. Print ();
}
Myinterface is the interface of myclass and only declares a method print (). myclass implements this method:
Public void print (){
System. Out. println ("hello ");
}
So if we want to output "Hello World!" without changing the original myclass !", What should I do?
Of course, we can directly write a subclass of myclass, such as helloclass. However, if you want to output "Hello world! ", My hello World", "My hello" and other combinations?
In inheritance mode, you have to write a bunch of similar sub-classes.
Decorator: the solution of the decoration mode is to only implement the basic functions and extract the additional functions.
For example, the following code:
Class decoratora implements decorator {
Myinterface myobject;
Decoratora (myinterface myobject ){
This. myobject = myobject;
}
Public void print (){
Myobject. Print ();
System. Out. Print ("world! ");
}
}
Class decoratorb implements decorator {
Myinterface myobject;
Decoratora (myinterface myobject ){
This. myobject = myobject;
}
Public void print (){
System. Out. Print ("my ");
Myobject. Print ();
}
}
The functions of decoratora and decoratorb are to print the world and my respectively. In this case, the main function will print out my Hello world, which can be simply changed:
Public static void main (strings [] Arg ){
Myinterface A = new decoratora (New decoratorb (New myclass ());
A. Print ();
}
Simple? Simply put, it is:
Print (){
Print ("XXX"); // can be replaced with any processing you want to add;
Myobject. Print (); // call the functions of the basic class;
Xxxx; // subsequent processing
}
The introduction of decorator ends here. Next we will talk about Java. Io.
See
Myinterface A = new decoratora (New decoratorb (New myclass ());
Do you think you are familiar with it? This follows
Bufferedinputstream Bis = new bufferedinputstream (New datainpustream (New fileinputstream ("xxx.txt ")));
Is it very similar?
(The voiceover is thrown with a stinking egg: Because Java. Io is organized in the decorator mode, of course, like ......)
Java. Io is divided into two categories: stream, reader, and writer. Here we will only detail the relationship between stream and the last two. Stream is divided into inputstream and outputstream, which are basically symmetric. Here we only introduce inputstream.
Java. Io. inputstream
|
_______________________ | ________________________
|
Bytearrayinputstream filterinputstream
Stringbufferinputstream _____________________ | ____________________________
Fileinputstream |
Pipedinputstream datainputstream bufferedinputstream linenuminpustream xxx
(Note: XXX is pushbackinputstream, and the figure above cannot be placed)
This figure is very similar to the "Hello World" image introduced at the beginning? Haha.
There are only four basic streams on the left. These streams represent the data source. All streams must start from one of the four. (Note: There is also a randomaccessfile and file, which are not covered in this article ).
Then, select a decoration from the right side when we need to add a function. For example, if we need the cache function, we need bufferedinputstream Decoration:
Bufferdinputstream is = new bufferedinputstream (New fileinputstream ("xxx.txt "));
If you want the datainputstream function, you only need to add one layer:
Datainputstream Dis = new datainputstream (New bufferdinputstream (New fileinputstream ));
(ER, I don't quite understand what the function added to this class is for. The document says it is to increase the function of reading Java Native data, but I don't quite understand it. I 'd like to add a clear complement to it, pipeinputstream and sequenceinputstream have never been used, please note)
As you can imagine, call the basic fileinputstream. readxxx () method in the bufferedinputstream readxxx () method and add corresponding processing.
Finally, let's talk about the difference between inputstream and Reader:
To be honest, I only know one byte stream and one Unicode stream. I don't know what the difference in the performance of rice is, haha. If you know it, come and add it ~