0.Adorner mode
The Java I/O class library requires a combination of many different functions, which is why the adorner pattern is used. This is why the filter (filter) class exists in the Java I/O class library, and the abstract class filter is the base class for all adorner classes. The adorner must have the same interface as the object it decorates , but it can also extend the interface, which only happens in individual filter classes.
However, the adorner pattern has one drawback: it gives us quite a lot of flexibility when writing programs (because we can easily mix and match attributes), but it also increases the complexity of the code . The reason the Java I/O class library is inconvenient is that we have to create many class one by one "core" I/O types plus all adorners to get the individual I/O objects we want.
1.
FilterlnputstreamAnd
Filteroutputstream
filterlnputstream and filteroutputstream are used to provide an adorner class interface to control a specific input stream (InputStream) and the two classes of the output stream (OutputStream), whose names are not very intuitive. Filterinput-stream and Filteoutputstream derive from the base class Inputstreain and OutputStream, respectively, from the I/O class library, which are the necessary conditions for adorners, which are two classes (in order to provide a common interface for all objects being modified).
(1) Filterlnputstream class in JDK API
(2) in the JDK API
FilteroutputstreamClass
2.BufferedInputStream & Bufferedoutputstream
Bufferedinputstream is an input stream with buffer, the default buffer size is 8M, can reduce the number of disk access, improve file read performance;
The bufferedoutputstream is an output stream with buffers that can improve the efficiency of writing files.
Bufferedinputstream and Bufferedoutputstream are sub-classes of FilterInputStream class and Filteroutputstream class respectively, which realizes the decorative design pattern. Here is an example demo using Bufferedinputstream and bufferedoutputstream to achieve photo copying :
1 Public classCopypicbybuffer {2 3 Public Static voidMain (string[] args)throwsIOException {4 /** 5 * Use Bufferedinputstream and bufferedoutputstream to achieve photo reproduction 6 */7Copypicbybuffer ();8}9 Ten Private Static voidCopypicbybuffer ()throwsIOException { One //TODO auto-generated method stub A //Demo buffer - //1. Creating an input Stream object -FileInputStream FIS =NewFileInputStream ("h:\\workspace\\testfile\\1.jpg"); theBufferedinputstream Bufis =NewBufferedinputstream (FIS);//Decorative design mode - - //2. Creating an output Stream object -FileOutputStream fos =NewFileOutputStream ("h:\\workspace\\testfile\\1-copybybuffer.jpg"); +Bufferedoutputstream Bufos =NewBufferedoutputstream (FOS);//Decorative design mode - + intby = 0; A while(by = Bufis.read ())! =-1) { atBufos.write (by); -Bufos.flush ();//Refresh -} -Fos.close (); -Fis.close (); -System.out.println ("copy is over! "); in} -} to
2017-12-31 content from Java Programming ideas and Intelligence podcast courses
Introduction to Bufferedinputstream and Bufferedoutputstream usage
Java I/O---Add properties and useful interfaces-filterlnputstream&filteroutputstream