Java 中的 mark() 和 reset() 方法
mark() and reset() method in Java
根据文档,
void mark(int readlimit): 标记此输入流中的当前位置。
PushbackInputStream的mark方法什么都不做.
void reset(): 将此流重新定位到上次对此输入流调用标记方法时的位置。
class PushbackInputStream 的方法 reset 什么都不做 除了抛出 IOException。
You can check above 'DOES NOTHING'. So, If this is the case, Why
and Where this is useful ? In which situation I can use above both the
methods ?
以下是示例:
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();
}
}
Above is the sample, But not getting what exactly both the methods
does. Please guide.
mark
和 reset
方法是可选操作,并非每个 InputStream 都需要支持。你可以打电话给 markSupported
看看是否有。
PushbackInputStream 不支持这些方法。
方法还在,因为它们是在InputStream
接口中定义的。也许是一个糟糕的设计决定(本可以添加到单独的界面),但事实就是如此。
根据文档,
void mark(int readlimit): 标记此输入流中的当前位置。 PushbackInputStream的mark方法什么都不做.
void reset(): 将此流重新定位到上次对此输入流调用标记方法时的位置。 class PushbackInputStream 的方法 reset 什么都不做 除了抛出 IOException。
You can check above 'DOES NOTHING'. So, If this is the case, Why and Where this is useful ? In which situation I can use above both the methods ?
以下是示例:
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();
}
}
Above is the sample, But not getting what exactly both the methods does. Please guide.
mark
和 reset
方法是可选操作,并非每个 InputStream 都需要支持。你可以打电话给 markSupported
看看是否有。
PushbackInputStream 不支持这些方法。
方法还在,因为它们是在InputStream
接口中定义的。也许是一个糟糕的设计决定(本可以添加到单独的界面),但事实就是如此。