- Java.io 包類
- Java.io - 首頁
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- Java.io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io 包額外內容
- Java.io - 介面
- Java.io - 異常
- Java.io 包有用資源
- Java.io - 討論
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