
- 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 - LineNumberReader skip(long n) method
Description
The Java LineNumberReader skip(long n) method skips l characters from the input stream while keeping track of line numbers. It does not skip full lines but individual characters, including newlines (\n), which can affect the line number count.
Declaration
Following is the declaration for java.io.LineNumberReader.skip(long n) method −
public long skip(long n)
Parameters
n − The number of characters to skip.
Return Value
The number of characters actually skipped.
Exception
IOException − If an I/O error occurs.
IllegalArgumentException − If n is negative.
Example - Usage of LineNumberReader skip(long n) method
The following example shows the usage of Java LineNumberReader skip(long n) method.
LineNumberReaderDemo.java
package com.tutorialspoint; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; public class LineNumberReaderDemo { public static void main(String[] args) throws IOException { FileReader fr = null; LineNumberReader lnr = null; int i; char c; try { // create new reader fr = new FileReader("test.txt"); lnr = new LineNumberReader(fr); // read till the end of the stream while((i = lnr.read())!=-1) { // skip one byte lnr.skip(1); // converts integer to char c = (char)i; // prints char System.out.print(c); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } finally { // closes the stream and releases system resources if(fr!=null) fr.close(); if(lnr!=null) lnr.close(); } } }
Output(Assuming test.txt contains "ABCDE")
Let us compile and run the above program, this will produce the following result−
ACE
Example - Skipping Characters and Tracking Line Numbers
The following example shows the usage of Java LineNumberReader skip(long n) method. This example skips 6 characters from the input and then reads the next line.
LineNumberReaderDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.LineNumberReader; import java.io.StringReader; public class LineNumberReaderDemo { public static void main(String[] args) { String text = "Hello\nWorld\nJava Programming"; LineNumberReader lnr = new LineNumberReader(new StringReader(text)); try { System.out.println("Reading first character: " + (char) lnr.read()); // Skip 6 characters lnr.skip(6); System.out.println("After skipping 6 characters, next character: " + (char) lnr.read()); System.out.println("Current Line Number: " + lnr.getLineNumber()); lnr.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output(if example.txt contains "HelloWorld")
Let us compile and run the above program, this will produce the following result−
Reading first character: H After skipping 6 characters, next character: W Current Line Number: 1
Explanation
Reads the first character (H).
Skips 6 characters, which includes "ello\nW".
Reads the next character, which is W from "World".
Line number updates to 1 because a newline (\n) was skipped.
Example - Skipping Over Multiple Lines
The following example shows the usage of Java LineNumberReader skip(long n) method. This example skips characters across multiple lines and checks how the line number changes.
LineNumberReaderDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.LineNumberReader; import java.io.StringReader; public class LineNumberReaderDemo { public static void main(String[] args) { String text = "First Line\nSecond Line\nThird Line"; LineNumberReader lnr = new LineNumberReader(new StringReader(text)); try { System.out.println("Initial Line Number: " + lnr.getLineNumber()); // Skip first 12 characters, including '\n' lnr.skip(12); System.out.println("After skipping 12 characters, next character: " + (char) lnr.read()); System.out.println("Line Number After Skip: " + lnr.getLineNumber()); lnr.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Initial Line Number: 0 After skipping 12 characters, next character: S Line Number After Skip: 1
Explanation
Line number starts at 0.
Skips 12 characters, covering "First Line\n".
The next character read is S from "Second Line".
Line number updates to 1 because a newline (\n) was skipped.