[Java中的mark()和reset()方法

问题描述 投票:1回答:1

根据文档,

void mark(int readlimit):标记此输入流中的当前位置。PushbackInputStream的标记方法不执行任何操作

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(); 
    } 
} 

上面是示例,但是两种方法都不能准确得到确实。请指导。

根据文档,void mark(int readlimit):标记此输入流中的当前位置。 PushbackInputStream的mark方法不执行任何操作。 void reset():重新定位此流...

java file java-stream reset pushbackinputstream
1个回答
2
投票

markreset方法是可选操作,并非每个InputStream都需要支持。您可以致电markSupported查明是否这样做。

© www.soinside.com 2019 - 2024. All rights reserved.