Home » Java programming language

Java RandomAccessFile readLine() Method with Example

RandomAccessFile Class readLine() method: Here, we are going to learn about the readLine() method of RandomAccessFile Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020

RandomAccessFile Class readLine() method

  • readLine() method is available in java.io package.
  • readLine() method is used to read the line of data from this RandomAccessFile and it continuously read bytes from the file beginning at the current file pointer until it indicates End-Of-Line or End-Of-File reached.
  • readLine() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • readLine() method may throw an exception at the time of the reading line of data.
    IOException: This exception may throw an exception while performing input/output operation.

Syntax:

    public final String readLine();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is String, it returns the next line of data from this file otherwise it returns null when it reaches EOF.

Example:

// Java program to demonstrate the example 
// of String readLine() method of
// RandomAccessFile

import java.io.*;

class RAFReadLine {
 public static void main(String[] args) throws Exception {
  // Instantiate a random access file
  // object with file name and permissions
  RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");

  // By using writeFloat() method is to 
  // write float to the file
  ra_f.writeUTF("Welcome, in Java World!!\n");

  // Initially set the file pointer
  // is at 1 for reading the file
  ra_f.seek(1);

  // By using readLine() method is to 
  // read a line of data from this file

  String line = ra_f.readLine();
  System.out.println("ra_f.readLine(): " + line);

  // By using close() method isto
  // close this stream ran_f
  ra_f.close();
 }
}

Output

ra_f.readLine(): ↓Welcome, in Java World!!



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.