Java IO Tutorial - Java RandomAccessFile.readShort()








Syntax

RandomAccessFile.readShort() has the following syntax.

public final short readShort()   throws IOException

Example

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

/*w  w  w.  j a  v a 2  s.co  m*/

import java.io.*;

public class Main {

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

         // write something in the file
         raf.writeShort(s);

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

         // print the short
         System.out.println(raf.readShort());

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

         // write something in the file
         raf.writeShort(134);

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

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

   }
}