Decorator Mode (Decorator pattern) Java IO class usage
This address: http://blog.csdn.net/caroline_wendy/article/details/26716823
Decorator Mode (decorator pattern) See: http://blog.csdn.net/caroline_wendy/article/details/26707033
the Java IO class is extended using decorator mode , where the FilterInputStream class is the base class for decorators (decorator) .
To implement other decorators (decorator), you need to inherit 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 LowerC Aseinputstream (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=offs Et i<offset+result; ++i) {B[i] = (byte) character.tolowercase ((char) b[i]);} return result;}}
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 ();}}}
format changes are implemented by decorating the
detailed component Class FileInputStream.
Note: The default read path for Java files is the root folder of the project.
Design pattern-Decorator mode (Decorator pattern) Java IO class usage