Java RandomAccessFile.readChar()

Syntax

RandomAccessFile.readChar() has the following syntax.

public final char readChar()   throws IOException

Example

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


/*from   w w w.  jav a2s  . co m*/

import java.io.*;

public class Main {

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

         // write something in the file
         raf.writeChar('A');

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

         // read char
         System.out.println(raf.readChar());

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

         // write a char at the start
         raf.writeChar(c);

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

         System.out.println(raf.readChar());
         raf.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }

   }
}