更新时间:2023-01-09 17:55:58
输入流是这样工作的:一旦你从中读取了一部分,它就永远消失了.你不能回去重新阅读它.你可以做的是这样的:
Input stream work like this: once you read a portion from it, it's gone forever. You can't go back and re-read it. what you could do is something like this:
class InputStreamSplitter {
InputStreamSplitter(InputStream toReadFrom) {
this.reader = new InputStreamReader(toReadFrom);
}
void addListener(Listener l) {
this.listeners.add(l);
}
void work() {
String line = this.reader.readLine();
while(line != null) {
for(Listener l : this.listeners) {
l.processLine(line);
}
}
}
}
interface Listener {
processLine(String line);
}
让所有感兴趣的各方实现 Listener 并将它们添加到 InputStreamSplitter
have all interested parties implement Listener and add them to InputStreamSplitter