目录

void reset()

描述 (Description)

java.io.LineNumberInputStream.reset()将此流重新定位到上次在此输入流上调用makr方法时的位置。

声明 (Declaration)

以下是java.io.LineNumberInputStream.reset()方法的声明 -

public void reset()

参数 (Parameters)

NA

返回值 (Return Value)

该方法不返回任何值。

异常 (Exception)

IOException - 如果发生I/O错误。

例子 (Example)

以下示例显示了java.io.LineNumberInputStream.reset()方法的用法。

package com.iowiki;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;
public class LineNumberInputStreamDemo {
   public static void main(String[] args) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis = null;
      try {
         // create new input streams
         fis = new FileInputStream("C:/test.txt");
         lnis = new LineNumberInputStream(fis);
         // reads and prints data from input stream
         System.out.println((char)lnis.read());
         System.out.println((char)lnis.read());
         // mark invoked at this position
         lnis.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)lnis.read());
         System.out.println((char)lnis.read());
         // if this input stream supports mark() an reset()
         if(lnis.markSupported()) {
            // reset() repositioned the stream to the mark
            lnis.reset();
            System.out.println("reset() invoked");
            System.out.println((char)lnis.read());
            System.out.println((char)lnis.read());
         }
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases any system resources
         if(fis!=null)
            fis.close();
         if(lnis!=null)
            lnis.close();      
      }
   }
}

假设我们有一个文本文件c:/test.txt ,它具有以下内容。 该文件将用作示例程序的输入 -

ABCDE

让我们编译并运行上面的程序,这将产生以下结果 -

A
B
mark() invoked
C
D
↑回到顶部↑
WIKI教程 @2018