【发布时间】:2019-10-04 04:33:19
【问题描述】:
根据文档,
void mark(int readlimit): 标记此输入流中的当前位置。 PushbackInputStream 的mark方法什么都不做。
void reset(): 将此流重新定位到上次在此输入流上调用标记方法时的位置。 PushbackInputStream 类的方法 reset 什么都不做,除了抛出一个 IOException。
您可以检查上面的“什么都不做”。所以,如果是这样的话,为什么 这在哪里有用?在哪种情况下我可以同时使用 方法?
下面是例子:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
public class PushbackInputStreamDemo
{
public static void main(String arg[]) throws Exception
{
PrintWriter pw = new PrintWriter(System.out, true);
String str = "GeeksforGeeks a computer science portal ";
byte b[] = str.getBytes();
ByteArrayInputStream bout = new ByteArrayInputStream(b);
PushbackInputStream push = new PushbackInputStream(bout);
int c;
while((c=push.read())!=-1)
{
pw.print((char)c);
}
pw.println();
// marking the position
push.mark(5);
// reseting is not supported throw exception
push.reset();
pw.close();
}
}
以上是示例,但没有得到这两种方法的具体内容 做。请指导。
【问题讨论】:
-
PushbackInputStream.markSupported()返回false。所以你不应该打电话给mark/reset。并且该类中mark的 javadocs 清楚地声明它什么都不做。 -
是的,我很困惑。如果它什么都不做,那么使用它有什么问题。顺便说一句,现在明白了。
标签: java file stream reset pushbackinputstream