Java - RandomAccessFile writeShort(int v) method



Description

The Java RandomAccessFile writeShort(int v) method writes a short to the file as two bytes, high byte first. The write starts at the current position of the file pointer.

writeShort(int v) method −

  • Writes the lower 2 bytes (16 bits) of the given int value.

  • Stored in big-endian order (high byte first).

  • The file pointer advances by 2 bytes.

  • Only the lower 16 bits of the int are written; the rest are ignored.

  • Can be read back using readShort() (returns a short).

Declaration

Following is the declaration for java.io.RandomAccessFile.writeShort(int v) method.

public final void writeShort(int v)

Parameters

v − a short value to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of RandomAccessFile writeShort(int v) method

The following example shows the usage of RandomAccessFile writeShort(int v) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {

      try {
         short s = 15;

         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

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

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

         // read short
         System.out.println("" + raf.readShort());

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

         // write a short at the start
         raf.writeShort(20);

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

         // read short
         System.out.println("" + raf.readShort());
         
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −

ABCDE

Let us compile and run the above program, this will produce the following result −

15
20

Example - Skipping Bytes Before Reading

The following example shows the usage of RandomAccessFile writeShort(int v) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try (RandomAccessFile raf = new RandomAccessFile("short1.dat", "rw")) {
         raf.writeShort(300);  // 300 fits in 16 bits
   
         raf.seek(0); // Reset file pointer
         short value = raf.readShort();

         System.out.println("Read short: " + value);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Read short: 300

Explanation

  • 300 is 0x012C in hex → written as bytes 01 2C.

  • readShort() reads both bytes and reconstructs the original value.

Example - Write Multiple Shorts and Read Them

The following example shows the usage of RandomAccessFile writeShort(int v) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try (RandomAccessFile raf = new RandomAccessFile("short2.dat", "rw")) {
         // Write three short values
         raf.writeShort(1000);
         raf.writeShort(2000);
         raf.writeShort(3000);

         raf.seek(0); // Reset file pointer

         short a = raf.readShort();
         short b = raf.readShort();
         short c = raf.readShort();

         System.out.println("Read shorts: " + a + ", " + b + ", " + c);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Read shorts: 1000, 2000, 3000

Explanation

  • Each short takes 2 bytes → total of 6 bytes written.

  • Values are written and read in sequence.

  • Internally −

    • 1000 → 0x03E8

    • 2000 → 0x07D0

    • 3000 → 0x0BB8

java_io_randomaccessfile.htm
Advertisements