装饰者模式-FilterInputStream
import java.io.*; public class LowerCaseInputStream extends FilterInputStream { /** * Creates a <code>FilterInputStream</code> * by assigning the argument <code>in</code> * to the field <code>this.in</code> so as * to remember it for later use. * * @param in the underlying input stream, or <code>null</code> if * this instance is to be created without an underlying stream. */ protected LowerCaseInputStream(InputStream in) { super(in); } @Override public int read() throws IOException { int c = super.read(); return (c == -1 ? c : Character.toLowerCase((char)c)); } @Override public int read(byte[] bytes, int offset, int len) throws IOException { int result = super.read(bytes, offset, len); for (int i = offset; i < offset + result; i++) { bytes[i] = (byte) Character.toLowerCase((char)bytes[i]); } return result; } public static void main(String[] args) { int c = 0; InputStream in = null; try { //设置 FileInputStream ,先用 BufferedInputStream 装饰它,再用 LowerCaseInputStream 进行装饰 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(); } } }
我只想安静地学习,捡拾前人的牙慧,默默强大如此弱小的我...

浙公网安备 33010602011771号