Java - ObjectOutputStream writeShort(int val) method



Description

The Java ObjectOutputStream writeShort(int val) method writes a 16 bit short.

  • Writes the lower 16 bits of the given int as a 2-byte (short) value to the output stream.

  • The value is written in big-endian (high byte first) format.

  • It's part of the DataOutput interface, which ObjectOutputStream implements.

  • Used to write compact binary data (not object serialization).

  • Paired with readShort() during deserialization.

Declaration

Following is the declaration for java.io.ObjectOutputStream.writeShort(int val) method.

public void writeShort(int val)

Parameters

  • val − The short value to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If I/O errors occur while writing to the underlying stream.

Example - Usage of ObjectOutputStream writeShort(int val) method

The following example shows the usage of ObjectOutputStream writeShort(int val) method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

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

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      short s = 174;
      short s2 = 1654;
      
      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(s2);

         // close the stream
         oout.close();

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

         // read and print what we wrote before
         System.out.println("" + ois.readShort());
         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 −

174
1654

Example - Write and read a single short value

The following example shows the usage of ObjectOutputStream writeShort(int val) method. We're writing the number 12345 as a 2-byte short and read it back.

ObjectOutputStreamDemo.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 ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "short1.bin";

      // Write a short value
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeShort(12345);
         System.out.println("Wrote short value: 12345");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read it back
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         short value = ois.readShort();
         System.out.println("Read short value: " + value);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote short value: 12345
Read short value: 12345

Example - Write multiple short values (e.g. port numbers)

The following example shows the usage of ObjectOutputStream writeShort(int val) method. We're write a list of port numbers using writeShort()

ObjectOutputStreamDemo.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 ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "ports.bin";
      int[] ports = {80, 443, 8080, 3306}; // All valid short values

      // Write short values
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         for (int port : ports) {
            oos.writeShort(port);
         }
         System.out.println("Wrote port numbers as short values.");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read back the short values
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         System.out.println("Reading ports:");
         for (int i = 0; i < ports.length; i++) {
            short port = ois.readShort();
            System.out.println(" - " + port);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote port numbers as short values.
Reading ports:
 - 80
 - 443
 - 8080
 - 3306
java_io_objectoutputstream.htm
Advertisements