根据文档,
void mark(int readlimit):标记此输入流中的当前位置。PushbackInputStream 的 mark 方法不执行任何操作。
void reset():将此流重新定位到上次在此输入流上调用 mark 方法时的位置。PushbackInputStream 类的重置方法除了抛出 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();
}
}
上面是示例,但没有得到这两种方法的确切作用。请指导。
猛跑小猪
相关分类