Design Pattern-Decorator Pattern Java IO class usage

Source: Internet
Author: User

Decorator Pattern Java I/O class usage

 

 

 

Java I/O classUseDecoration ModeAndFilterInputStream classThat isDecorator base class.

Implement otherDecorator), RequiredInherit the FilterInputStream class.

 

Code:

 

/*** @ Time May 23, 2014 */package decorator. io; import java. io. filterInputStream; import java. io. IOException; import java. io. inputStream;/*** @ author C. l. wang **/public class LowerCaseInputStream extends FilterInputStream {public LowerCaseInputStream (InputStream in) {super (in);} public int read () throws IOException {int c = super. read (); return (c =-1? C: Character. toLowerCase (char) c);} public int read (byte [] B, int offset, int len) throws IOException {int result = super. read (B, offset, len); for (int I = offset; I
 
  Test:
  

 

 

/*** @ Time May 23, 2014 */package decorator. io; import java. io. bufferedInputStream; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream;/*** @ author C. l. wang **/public class InputTest {/*** @ param args */public static void main (String [] args) throws IOException {// TODO Auto-generated method stubint c; try {InputStream in = new LowerCaseInputStream (new BufferedInputStream (new FileInputStream(test.txt); while (c = in. read ()> = 0) {System. out. print (char) c);} in. close ();} catch (IOException e) {e. printStackTrace ();}}}

Through Decoration FileInputStreamTo change the format.

 

Note:The default reading path of Java files is the project root directory.

 

 

 

 

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: info-contact@alibabacloud.com 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.