Java - ObjectInputStream readShort() method



Description

The Java ObjectInputStream readShort() method reads a 16 bit short.

Declaration

Following is the declaration for java.io.ObjectInputStream.readShort() method −

public short readShort()

Parameters

NA

Return Value

This method returns the 16 bit short read.

Exception

  • EOFException − If end of file is reached.

  • IOException − If other I/O error has occurred.

Example - Usage of ObjectInputStream readShort() method

The following example shows the usage of Java ObjectInputStream readShort() method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      short s = 56;
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeShort(s);
         oout.writeShort(new Short("1"));
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print a short
         System.out.println("" + ois.readShort());

         // read and print a short
         System.out.println("" + ois.readShort());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

56
1

Example - Reading a Short Value from a File

The following example shows the usage of Java ObjectInputStream readShort() method. This example writes a short value to a file using ObjectOutputStream and then reads it back using ObjectInputStream.readShort().

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
    public static void main(String[] args) {
        String filename = "shortData.bin";

        // Writing a short value to a file
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
            short num = 32000;
            oos.writeShort(num);
            System.out.println("Short value written: " + num);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Reading the short value from the file
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
            short readNum = ois.readShort();
            System.out.println("Short value read: " + readNum);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output

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

Short value written: 32000
Short value read: 32000

Explanation

  • A short value (32000) is written to a file using ObjectOutputStream.writeShort().

  • The program then reads the short value from the file using ObjectInputStream.readShort().

  • The retrieved value is printed to verify correctness.

Example - Reading Multiple Short Values from a File

The following example shows the usage of Java ObjectInputStream readShort() method. This example writes multiple short values to a file and reads them back sequentially.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      String filename = "multiShortData.bin";

      // Writing multiple short values to a file
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         short[] numbers = {1000, 2000, 3000, 4000};

         for (short num : numbers) {
            oos.writeShort(num);
         }
         System.out.println("Short values written.");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Reading multiple short values from the file
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         while (true) {
            try {
               short readNum = ois.readShort();
               System.out.println("Short value read: " + readNum);
            } catch (EOFException e) {
               break; // End of file reached
            }
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Short values written.
Short value read: 1000
Short value read: 2000
Short value read: 3000
Short value read: 4000

Explanation

  • An array of short values (1000, 2000, 3000, 4000) is written to a file using ObjectOutputStream.writeShort().

  • The program then reads each short value using ObjectInputStream.readShort(), printing them one by one.

  • A while loop continues reading until an EOFException is caught, indicating the end of the file.

java_io_objectinputstream.htm
Advertisements