reset()方法是Java.io.ByteArrayInputStream的內置方法,由mark()方法調用。它將輸入流重新定位到標記的位置。
用法:
public void reset()
參數:該函數不接受任何參數。
返回值:該函數不返回任何內容。
下麵是上述函數的實現:
示例1:
// Java program to implement
// the above function
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception
{
byte[] buf = { 5, 6, 7, 8, 9 };
// Create new byte array input stream
ByteArrayInputStream exam
= new ByteArrayInputStream(buf);
// print bytes
System.out.println(exam.read());
System.out.println(exam.read());
System.out.println(exam.read());
// Use of reset() method :
// repositioning the stram to marked positions.
exam.reset();
System.out.println("\nreset() invoked");
System.out.println(exam.read());
System.out.println(exam.read());
}
}
輸出:
5 6 7 reset() invoked 5 6
示例2:
// Java program to implement
// the above function
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception
{
byte[] buf = { 1, 2, 3, 4 };
// Create new byte array input stream
ByteArrayInputStream exam
= new ByteArrayInputStream(buf);
// print bytes
System.out.println(exam.read());
System.out.println(exam.read());
System.out.println(exam.read());
exam.mark(1);
// Use of reset() method :
// repositioning the stram to marked positions.
exam.reset();
System.out.println("\nreset() invoked");
System.out.println(exam.read());
System.out.println(exam.read());
}
}
輸出:
1 2 3 reset() invoked 4 -1
參考: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#reset()
相關用法
- Java ByteArrayInputStream available()用法及代碼示例
- Java ByteArrayInputStream skip()用法及代碼示例
- Java ByteArrayInputStream close()用法及代碼示例
- Java ByteArrayInputStream markSupported()用法及代碼示例
- Java ByteArrayInputStream mark()用法及代碼示例
- Java LongAdder reset()用法及代碼示例
- Java PushbackReader reset()用法及代碼示例
- Java LongAccumulator reset()用法及代碼示例
- Java DoubleAccumulator reset()用法及代碼示例
- Java DoubleAdder reset()用法及代碼示例
- Java LogManager reset()用法及代碼示例
- Java Matcher reset()用法及代碼示例
- Java CharsetEncoder reset()用法及代碼示例
- Java MessageDigest reset()用法及代碼示例
- Java CharsetDecoder reset()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 ByteArrayInputStream reset() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。