Java I/O---Add properties and useful interfaces-filterlnputstream&filteroutputstream

Source: Internet
Author: User

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 = ())! =-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

Related Article

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: 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.