try (InputStream inputStream = new FileInputStream("example.txt")) { inputStream.mark(1024); int data = inputStream.read(); // process data inputStream.reset(); // the stream is now back to the mark } catch (IOException ex) { // handle exception }
try (InputStream inputStream = new FileInputStream("example.txt")) { if (inputStream.markSupported()) { inputStream.mark(1024); } // read data from the stream } catch (IOException ex) { // handle exception }In this example, the markSupported() method is called to determine whether the input stream supports the mark() method. If it does, the mark() method is called with a buffer size of 1024 bytes. Data is then read from the stream. If the mark() method is not supported, the stream cannot be marked and the code will continue reading data without the ability to rewind. The InputStream class is part of the java.io package library in Java.