Java - RandomAccessFile readLine() method



Description

The Java RandomAccessFile readLine() method reads the next line of text from this file. This method successively reads bytes from the file, starting at the current file pointer, until it reaches a line terminator or the end of the file. Each byte is converted into a character by taking the byte's value for the lower eight bits of the character and setting the high eight bits of the character to zero.

Declaration

Following is the declaration for java.io.RandomAccessFile.readLine() method.

public final String readLine()

Parameters

NA

Return Value

This method returns the next line of text from this file, or null if end of file is encountered before even one byte is read.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of RandomAccessFile readLine() method

The following example shows the usage of RandomAccessFile readLine() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
   
      try {
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write something in the file
         raf.writeUTF("Hello World");

         // set the file pointer at 0 position
         raf.seek(0);

         // print the line
         System.out.println(raf.readLine());

         // set the file pointer at 0 position
         raf.seek(0);

         // write something in the file
         raf.writeUTF("This is an example \n Hello World");

         raf.seek(0);
         // print the line
         System.out.println(raf.readLine());
         
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −

ABCDE

Let us compile and run the above program, this will produce the following result −

Hello World
This is an example

Example - Reading Lines from a Text File

The following example shows the usage of RandomAccessFile readLine() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("lines1.txt", "rw");

         // Write lines to the file
         raf.writeBytes("Hello\n");
         raf.writeBytes("World\n");

         // Reset file pointer to beginning
         raf.seek(0);

         // Read lines
         String line1 = raf.readLine();
         String line2 = raf.readLine();

         System.out.println("Line 1: " + line1); // Hello
         System.out.println("Line 2: " + line2); // World

         raf.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Line 1: Hello
Line 2: World

Explanation

  • writeBytes() writes raw byte strings.

  • readLine() reads text up to a newline (\n or \r\n) and returns it as a String.

  • It's useful for reading plain-text files line by line

Example - Jump to a Specific Line

The following example shows the usage of RandomAccessFile readLine() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("lines2.txt", "rw");

         // Write three lines
         raf.writeBytes("Apple\n");
         raf.writeBytes("Banana\n");
         raf.writeBytes("Cherry\n");

         // Move to the beginning
         raf.seek(0);

         // Skip first line
         raf.readLine(); // Reads "Apple"

         // Read second line
         String secondLine = raf.readLine();

         System.out.println("Second line: " + secondLine); // Banana

         raf.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Second line: Banana

Explanation

  • We use readLine() to skip the first line.

  • The second call to readLine() gets the second line.

  • This shows how you can navigate through lines manually with RandomAccessFile.

java_io_randomaccessfile.htm
Advertisements