
- 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 - LineNumberInputStream reset() method
Description
The Java LineNumberInputStream reset() method repositions this stream to the position at the time the mark() method was last called on this input stream.
Declaration
Following is the declaration for java.io.LineNumberInputStream.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 LineNumberInputStream reset() method
The following example shows the usage of Java LineNumberInputStream reset() method.
LineNumberInputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.LineNumberInputStream; public class LineNumberInputStreamDemo { public static void main(String[] args) throws IOException { LineNumberInputStream lnis = null; FileInputStream fis = null; try { // create new input streams fis = new FileInputStream("test.txt"); lnis = new LineNumberInputStream(fis); // reads and prints data from input stream System.out.println((char)lnis.read()); System.out.println((char)lnis.read()); // mark invoked at this position lnis.mark(0); System.out.println("mark() invoked"); System.out.println((char)lnis.read()); System.out.println((char)lnis.read()); // if this input stream supports mark() an reset() if(lnis.markSupported()) { // reset() repositioned the stream to the mark lnis.reset(); System.out.println("reset() invoked"); System.out.println((char)lnis.read()); System.out.println((char)lnis.read()); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } finally { // closes the stream and releases any system resources if(fis!=null) fis.close(); if(lnis!=null) lnis.close(); } } }
Output(Assuming test.txt contains "ABCDE")
Let us compile and run the above program, this will produce the following result−
A B mark() invoked C D
Example - Resetting After Reading Some Bytes
The following example shows the usage of Java LineNumberInputStream reset() method. This example demonstrates marking a position in the stream, reading a few characters, and then using reset() to return to the marked position.
LineNumberInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.LineNumberInputStream; public class LineNumberInputStreamDemo { public static void main(String[] args) { String text = "Hello\nWorld\nJava"; byte[] data = text.getBytes(); try { ByteArrayInputStream bais = new ByteArrayInputStream(data); LineNumberInputStream lnis = new LineNumberInputStream(bais); lnis.setLineNumber(1); // Start line numbering from 1 lnis.mark(10); // Mark the current position System.out.println("Reading first character: " + (char) lnis.read()); System.out.println("Current Line Number: " + lnis.getLineNumber()); System.out.println("Reading second character: " + (char) lnis.read()); System.out.println("Current Line Number: " + lnis.getLineNumber()); lnis.reset(); // Reset to the marked position System.out.println("After reset, reading again: " + (char) lnis.read()); System.out.println("Current Line Number after reset: " + lnis.getLineNumber()); lnis.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Reading first character: H Current Line Number: 1 Reading second character: e Current Line Number: 1 After reset, reading again: H Current Line Number after reset: 1
Explanation
mark(10) marks the position in the stream.
Reads characters 'H' and 'e', but the line number remains 1.
Calls reset(), returning to the marked position.
Reads from the beginning again, confirming that the reset worked.
Example - Resetting After Reading Multiple Lines
The following example shows the usage of Java LineNumberInputStream reset() method. This example shows how resetting affects line numbers when reading multiple lines.
LineNumberInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.LineNumberInputStream; public class LineNumberInputStreamDemo { public static void main(String[] args) { String text = "First Line\nSecond Line\nThird Line"; byte[] data = text.getBytes(); try { ByteArrayInputStream bais = new ByteArrayInputStream(data); LineNumberInputStream lnis = new LineNumberInputStream(bais); lnis.setLineNumber(1); // Set initial line number lnis.mark(20); // Mark the position before reading lines byte[] buffer = new byte[15]; lnis.read(buffer, 0, buffer.length); System.out.println("Read Data: " + new String(buffer)); System.out.println("Line Number Before Reset: " + lnis.getLineNumber()); lnis.reset(); // Reset to marked position buffer = new byte[15]; lnis.read(buffer, 0, buffer.length); System.out.println("After reset, Read Data Again: " + new String(buffer)); System.out.println("Line Number After Reset: " + lnis.getLineNumber()); lnis.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Read Data: First Line Seco Line Number Before Reset: 2 After reset, Read Data Again: First Line Seco Line Number After Reset: 2
Explanation
Marks the position before reading.
Reads 15 bytes, covering First Line\nSecond.
The line number updates as newlines (\n) are encountered.
Resets the stream, returning to the marked position.
Reads again from the same point, verifying that both the position and line number reset correctly.