Метод сброса ByteArrayInputStream () в Java с примерами

Опубликовано: 7 Февраля, 2022

Метод reset () - это встроенный метод Java.io. ByteArrayInputStream вызывается методом mark (). Он перемещает входной поток в отмеченную позицию.

Синтаксис :

 public void reset ()

Параметры : функция не принимает никаких параметров.

Возвращаемое значение : функция ничего не возвращает.

Ниже представлена реализация вышеуказанной функции:

Program 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(" reset() invoked");
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}
Output:
5
6
7

reset() invoked
5
6

Program 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(" reset() invoked");
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}
Output:
1
2
3

reset() invoked
4
-1

Ссылка: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#reset ()

Вниманию читателя! Не переставай учиться сейчас. Ознакомьтесь со всеми важными концепциями Java Foundation и коллекций с помощью курса "Основы Java и Java Collections" по доступной для студентов цене и будьте готовы к работе в отрасли. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .