?
Three key knowledge points to learn from Java I / O libraries
Everyone discusses the Java I / O library for a long time, Palesting has just happened <
> Series, but I am estimated that some readers will feel that the Java I / O library is difficult to ponder, so I will come together and talk about the three key knowledge points I think of learning Java I / O libraries.
Knowledge point: four major grade structures
The Java language I / O library provides four levels: InputStream, OutputStream, Reader, Writer four series of classes. INPUTSTREAM and OUTPUTSTREAM processes 8-bit word throttle data, Reader, and Writer to process 16-bit character stream data. InputStream and Reader Processing Input, OutputStream and Writer Processing Output. Everyone must go to the J2SE documentation to see the class inheritance system of these four levels.
In addition to these four series, I / O libraries also provide a few auxiliary class, which is more important to InputStreamReader and OutputStreamWriter. InputStreamReader adapts the InputStream to Reader, OutputStreamWrit adapted OutputStream to Writer; this racks the bridge between the byte stream processing class and the character stream processing.
When you use the I / O library, just press the above rules to find the class you need in the appropriate class system.
Knowledge point 2: adaptation function
The inheritance in the Java I / O library is not ordinary inheritance; its inheritance mode is divided into two kinds, one is Adapter mode (see you for specific analysis.
> A book). The following is an example of an InputStream class system as an example.
InputStream has 7 direct subclasses: ByteaRrayInputStream, FileInputStream, PipedinputStream, StringBufferInputStream, FilterInputStream, ObjectInputStream, and SequenceInputStream. The first four adapter mode, such as the FileInputStream part of the code as follows:
Public Class FileInputStream Extends InputStream
{
??? / * file descriptor - handle to the open file * /
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.
??? public fileinputstream (FileDescriptor fdobj)
? {
??? SecurityManager security = system.getsecuritymanager ();
?? ration (fdobj == null) {
???? throw new nullpointerException ();
??}
?? i (security! = null) {
???? SECURITY.CHECKREAD (FDOBJ);
??}
???? fd = fdobj;
?
? // Other code
}
It can be seen that FILEINPUTSTREAM inherits the InputStream, combines FileDescriptor, which is an object adapter mode. When we learn I / O libraries, we should mainly master these four object adapter mode adaptation sources: BYTEARRAYINPUTSTREAM's adapter source is a BYTE array, and the adapter source of FileInputStream is a File object. The adapter source of PipedinputStream is the PipedputStream object, StringBufferInputStream The adaptation source is a String object. The learning methods of the three series classes are the same.
Knowledge 3: Decorator function
The other three direct subclasses of InputStream are Decorator mode, <
> The description is more clear. In fact, we don't need to manage what model it uses, and look at the code. The FilterInputStream part of the code is as follows:
Public class filterinputStream Extends InputStream {??? / **
???? * The input stream to be filtered.
???? * /
??? protected inputstream in;
??? protected filterinputstream (InputStream IN) {
????? this.in = in;
???}
??? // Other code
}
Clear? FilterInputStream inherits the InputStream and references InputStream, and it has four subclasses, which is called Decorator mode. We temporarily use Decorator mode, but we should now know: because InputStream has several other subclasses, we can construct other subclasses as parameters! This is the function, code for us to develop. Examples are as follows:
{
/ / Read the key from the key file
?? SECRETKEY key = null; ??
?? ObjectInputStream KeyFile = New ObjectInputStream
??????? New fileinputstream ("c: // security file // symmetry key //yhb.des"));
??? k = (secretkey) keyfile.readObject ();
??? kyfile.close ();
??}
In the above code, ObjectInputStream is also subclasses of InputStream, but also uses Decorator features, however, even if you don't understand Decorator mode, just remember the two-segment code for the FilterInputStream given in this article.
Master the above three points, I believe we have been able to apply the Java I / O library well.
?
HONGBO781202, also known as hongsoft, professional programmer, research area: 1) Based on workflow 2) Java-based information security
Technology. Welcome to discuss all aspects of Java related aspects of technology and market issues hongbosoftware@163.com