Java IO Tutorial - Java RandomAccessFile.readInt()








Syntax

RandomAccessFile.readInt() has the following syntax.

public final int readInt()   throws IOException

Example

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

// w ww .  j av  a  2s  . c  om
import java.io.*;

public class Main {

   public static void main(String[] args) {
      try {
         int i = 123;
         
         RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

         // write something in the file
         raf.writeInt(123);

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

         // print the int
         System.out.println(raf.readInt());

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

         // write something in the file
         raf.writeInt(i);

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

         // print the int
         System.out.println(raf.readInt());
         raf.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }


   }
}