countreader.java

来自「苏州大学java学习的ppt课件」· Java 代码 · 共 53 行

JAVA
53
字号
import java.io.*;public class CountReader extends FilterReader {    private int count = 0;    private char lookFor = 0;    public CountReader(Reader in, char lookFor) {        super(in);        this.lookFor = lookFor;    }    public int read() throws IOException {        int character = super.read();        if ((char)character == lookFor) {            count++;        }	return character;    }    public int read(char[] cbuf) throws IOException {        int numChars = super.read(cbuf);        int length = cbuf.length;        for (int i = 0; i < length; i++) {            if (cbuf[i] == lookFor) {                count++;            }        }	return numChars;    }    public int read(char[] cbuf, int off, int len) throws IOException {        int numChars = super.read(cbuf, off, len);        int length = cbuf.length;        for (int i = 0; i < length; i++) {            if (cbuf[i] == lookFor) {                count++;            }        }	return numChars;    }    public void reset() throws IOException {        super.reset();        count = 0;    }    public boolean markSupported() {        return false;    }    public void mark(int readAheadLimit) throws IOException {        throw new IOException("CountReader does not support the mark() operation.");    }    public int getCount() {        return count;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?