Java - ByteArrayInputStream markSupported()



描述

Java ByteArrayInputStream markSupported() 方法返回 true,如果輸入流支援 mark() 和 reset() 方法呼叫。對於 ByteArrayInputStream,該方法始終返回 true

宣告

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

public boolean markSupported()

引數

返回值

此方法對於 ByteArrayInputStream 返回 true

異常

示例 1

以下示例演示了 Java ByteArrayInputStream markSupported() 方法的用法。我們建立了一個名為 buf 的 byte[] 變數並用一些位元組初始化它。我們建立了一個 ByteArrayInputStream 引用,然後用 buf 變數初始化它。然後我們使用 markSupported() 方法檢查是否支援 mark。我們使用 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);
		 
         // test support for mark() and reset() methods invocation
         boolean isMarkSupported = bais.markSupported();
         System.out.println("Is mark supported : "+isMarkSupported);
         
         // 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();
      }   
   }
}

輸出

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

Is mark supported : true
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 markSupported() 方法的用法。我們建立了一個名為 buf 的 byte[] 變數並用一些位元組初始化它。我們建立了一個 ByteArrayInputStream 引用,然後用 buf 變數初始化它。然後我們使用 markSupported() 方法檢查是否支援 mark。我們使用 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);
		 
         // test support for mark() and reset() methods invocation
         boolean isMarkSupported = bais.markSupported();
         System.out.println("Is mark supported : "+isMarkSupported);
         
         // 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();
      }   
   }
}

輸出

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

Is mark supported : true
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
廣告
© . All rights reserved.