The Java IO class expands using the adorner pattern, where the FilterInputStream class is the base class for the adorner (decorator).
To implement other adorners (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 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 = Super.read (b, offset, len);
for (int i=offset; 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 stub
int C;
try{
InputStream in = new Lowercaseinputstream (New Bufferedinputstream ("FileInputStream"));
while ((c = in.read ()) >= 0) {
System.out.print ((char) c);
In.close ();
} catch (IOException e) {
e.printstacktrace ();}}}
By decorating the concrete component class FileInputStream, realize the format change.
Note: The default read path for Java files is the root directory of the project.
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/