
- Java.io - Home
- 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
- 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 package Useful Resources
- Java.io - Discussion
Java - InputStream reset() method
Description
The Java InputStream reset() moves the stream back to the last marked position, allowing the stream to re-read data from an earlier point. It works only if the stream supports marking (markSupported() should return true) and if mark(int readlimit) was called before reset().
Declaration
Following is the declaration for java.io.InputStream.reset() method −
public void reset()
Parameters
NA
Return Value
The method does not return any value.
Exception
IOException − If an I/O error occurs.
Example - Usage of InputStream reset() method
The following example shows the usage of Java InputStream reset() method.
InputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) throws Exception { InputStream is = null; try { // new input stream created is = new FileInputStream("test.txt"); System.out.println("Characters printed:"); // create new buffered reader // reads and prints BufferedReader System.out.println((char)is.read()); System.out.println((char)is.read()); // mark invoked at this position is.mark(0); System.out.println("mark() invoked"); System.out.println((char)is.read()); System.out.println((char)is.read()); // reset() repositioned the stream to the mark if(is.markSupported()) { is.reset(); System.out.println("reset() invoked"); System.out.println((char)is.read()); System.out.println((char)is.read()); } else { System.out.print("InputStream does not support reset()"); } } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases system resources associated with this stream if(is!=null) is.close(); } } }
Output(Assuming test.txt contains "ABCDE")
Let us compile and run the above program, this will produce the following result−
Characters printed: A B mark() invoked C D InputStream does not support reset()
Example - Using reset() with BufferedInputStream
The following example shows the usage of Java InputStream reset() method.
InputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) { try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) { System.out.println("Mark supported? " + inputStream.markSupported()); // Read and print the first character System.out.print((char) inputStream.read()); // Mark the current position inputStream.mark(5); // Allows reading up to 5 bytes before mark expires // Read next two characters System.out.print((char) inputStream.read()); System.out.print((char) inputStream.read()); // Reset back to the marked position inputStream.reset(); // Read again from the marked position System.out.print((char) inputStream.read()); System.out.print((char) inputStream.read()); } catch (IOException e) { e.printStackTrace(); } } }
Output(if example.txt contains "Hello")
Let us compile and run the above program, this will produce the following result−
Mark supported? true Helo elo
Explanation
Uses BufferedInputStream, which supports marking.
Calls markSupported(), which returns true.
Reads one character and then marks the position.
Reads two more characters, then resets to the marked position.
Reads again from the marked position.
Example - Using reset() with ByteArrayInputStream
The following example shows the usage of Java InputStream reset() method.
InputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) throws IOException{ byte[] data = "Microservices".getBytes(); InputStream inputStream = new ByteArrayInputStream(data); System.out.println("Mark supported? " + inputStream.markSupported()); inputStream.mark(6); // Mark at the beginning System.out.print((char) inputStream.read()); // Read 'M' System.out.print((char) inputStream.read()); // Read 'i' inputStream.reset(); // Reset to the marked position System.out.print((char) inputStream.read()); // Read 'M' again try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Mark supported? true MiM
Explanation
Uses ByteArrayInputStream, which supports marking.
Calls markSupported(), which returns true.
Marks at the beginning of the input stream.
Reads two characters (M and i).
Resets to the marked position and reads M again.