
- 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 read() method
Description
The Java ObjectInputStream read() method reads a single byte or an array of bytes from the input stream. It is used for reading raw byte data rather than deserializing objects.
Declaration
Following is the declaration for java.io.ObjectInputStream.read() method −
public int read()
Parameters
NA
Return Value
This method returns the byte read, or -1 if the end of the stream is reached.
Exception
IOException − If an I/O error has occurred.
Example - Usage of ObjectInputStream read() method
The following example shows the usage of Java ObjectInputStream read() 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) { 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.writeUTF("Hello World"); oout.flush(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read from the stream for (int i = 0; i < ois.available();) { System.out.print("" + (char) ois.read()); } } catch (Exception ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Hello World
Example - Reading One Byte at a Time
The following example shows the usage of Java ObjectInputStream read() method. This example writes raw bytes to a file and then reads them one byte at a time using ObjectInputStream.read().
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) { try { // Writing raw bytes to a file FileOutputStream fos = new FileOutputStream("data.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.write(65); // Writing 'A' (ASCII 65) oos.write(66); // Writing 'B' (ASCII 66) oos.write(67); // Writing 'C' (ASCII 67) oos.close(); // Reading bytes using ObjectInputStream FileInputStream fis = new FileInputStream("data.dat"); ObjectInputStream ois = new ObjectInputStream(fis); int byteRead; while ((byteRead = ois.read()) != -1) { // Read until end of stream System.out.println("Read byte: " + byteRead + " (" + (char) byteRead + ")"); } ois.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Read byte: 65 (A) Read byte: 66 (B) Read byte: 67 (C)
Explanation
Writes raw bytes (65, 66, 67) to a file (data.dat).
Reads bytes one by one using read() until EOF (-1) is reached.
Each byte is printed as its ASCII character.
Example - Reading Multiple Bytes into a Byte Array
The following example shows the usage of Java ObjectInputStream read() method. This example writes an array of bytes and reads them using read(byte[] b, int off, int len).
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) { try { // Writing byte array to a file FileOutputStream fos = new FileOutputStream("data.bin"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.write(new byte[]{72, 101, 108, 108, 111}); // "Hello" oos.close(); // Reading bytes using ObjectInputStream FileInputStream fis = new FileInputStream("data.bin"); ObjectInputStream ois = new ObjectInputStream(fis); byte[] buffer = new byte[5]; // Buffer to hold 5 bytes int bytesRead = ois.read(buffer, 0, buffer.length); System.out.println("Bytes Read: " + bytesRead); System.out.println("Read String: " + new String(buffer)); ois.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Bytes Read: 5 Read String: Hello
Explanation
Writes the byte array {72, 101, 108, 108, 111} ("Hello") to a file.
Uses read(byte[] b, int off, int len) to read 5 bytes into a buffer.
Converts the byte array to a string and prints "Hello".