Java RandomAccessFile.readLine()

Syntax

RandomAccessFile.readLine() has the following syntax.

public final String readLine()   throws IOException

Example

In the following code shows how to use RandomAccessFile.readLine() method.


/*from   w  w  w.ja va 2  s. com*/

import java.io.*;

public class Main {

   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("c:/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);

         raf.writeUTF("This is an example \n Hello World");

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

   }
}