书上边有段代码如下LineNumberInputStream li =
new LineNumberInputStream(
new StringBufferInputStream(s2));
DataInputStream in4 = new DataInputStream(li);然后书上说不能如下代码DataInputStream in4 =
new DataInputStream(
new LineNumberInputStream(
new StringBufferInputStream(s2)));书上给出的理由是:在这里,不可简单地将所有构建器都组合起来,因为必须保持LineNumberInputStream的一个句柄(注意这并非一种基础环境,所以不能简单地将in4造型到一个LineNumberInputStream)。
问:这哪里需要把in4造型到一个LineNumberInputStream了?看的我糊里糊涂,哪位能解释下谢谢
new LineNumberInputStream(
new StringBufferInputStream(s2));
DataInputStream in4 = new DataInputStream(li);然后书上说不能如下代码DataInputStream in4 =
new DataInputStream(
new LineNumberInputStream(
new StringBufferInputStream(s2)));书上给出的理由是:在这里,不可简单地将所有构建器都组合起来,因为必须保持LineNumberInputStream的一个句柄(注意这并非一种基础环境,所以不能简单地将in4造型到一个LineNumberInputStream)。
问:这哪里需要把in4造型到一个LineNumberInputStream了?看的我糊里糊涂,哪位能解释下谢谢
LineNumberInputStream li = new LineNumberInputStream( new StringBufferInputStream(s2));
DataInputStream in4 = new DataInputStream(li);
PrintStream out1 = new PrintStream(new BufferedOutputStream(new FileOutputStream("10Demo.out")));
while((s = in4.readLine()) !=null)
out1.println("Line " + li.getLineNumber() + s);
out1.close();
} catch(EOFException e) {
System.out.println("End of stream encounterde");
}这是这个小片段的代码 难道是因为下边要用到 li.getLineNumber() ?LineNumberInputStream没有readLine()方法吗 ? 为什么要进行 (s = in4.readLine()) !=null这步比较呢?不能换成(s =li.readLine()) !=null 吗?
LineNumberInputStream li = new LineNumberInputStream( new StringBufferInputStream(s2));
DataInputStream in4 = new DataInputStream(li);
PrintStream out1 = new PrintStream(new BufferedOutputStream(new FileOutputStream("10Demo.out")));
while((s = in4.readLine()) !=null)
out1.println("Line " + li.getLineNumber() + s);
out1.close();
} catch(EOFException e) {
System.out.println("End of stream encounterde");
}这是这个小片段的代码 难道是因为下边要用到 li.getLineNumber() ?LineNumberInputStream没有readLine()方法吗 ? 为什么要进行 (s = in4.readLine()) !=null这步比较呢?不能换成(s =li.readLine()) !=null 吗?