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.