Java - ByteArrayInputStream reset()



描述

Java ByteArrayInputStream reset() 方法將緩衝區重置到最後標記的位置。如果未顯式指定標記,則標記位置為 0。

宣告

以下是 java.io.ByteArrayInputStream.reset() 方法的宣告:

public void reset()

引數

返回值

此方法不返回值。

異常

示例 1

以下示例演示了 Java ByteArrayInputStream reset() 方法的使用。我們建立了一個名為 buf 的 byte[] 變數,並用一些位元組初始化它。我們建立了一個 ByteArrayInputStream 引用,然後用 buf 變數初始化它。我們使用 read() 方法讀取前三個位元組,然後使用 mark() 方法重置標記當前位置,然後再次讀取位元組。然後我們呼叫 reset() 方法將頭部重置到先前標記的位置,並再次讀取位元組。

package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArrayInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         // print bytes
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         
         System.out.println("Mark() invocation");

         // mark() invocation;
         bais.mark(0);
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         
         System.out.println("Reset() invocation");
         
         // reset() invocation
         bais.reset();
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Byte read 65
Byte read 66
Byte read 67
Mark() invocation
Byte read 68
Byte read 69
Reset() invocation
Byte read 68
Byte read 69

示例 2

以下示例演示了 Java ByteArrayInputStream reset() 方法的使用。我們建立了一個名為 buf 的 byte[] 變數,並用一些位元組初始化它。我們建立了一個 ByteArrayInputStream 引用,然後用 buf 變數初始化它。我們使用 read() 方法讀取前兩個位元組,然後使用 mark() 方法重置標記到當前位置,然後再次讀取位元組。然後我們呼叫 reset() 方法將頭部重置到先前標記的位置,並再次讀取位元組。

package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArrayInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         // print bytes
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         
         System.out.println("Mark() invocation");

         // mark() invocation;
         bais.mark(0);
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         
         System.out.println("Reset() invocation");
         
         // reset() invocation
         bais.reset();
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Byte read 65
Byte read 66
Mark() invocation
Byte read 67
Byte read 68
Byte read 69
Reset() invocation
Byte read 67
Byte read 68
java_bytearrayinputstream.htm
廣告