- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.