
- 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(byte[] buf, int off, int len) method
Description
The Java ObjectInputStream read(byte[] buf, int off, int len) method reads into an array of bytes. This method will block until some input is available. Consider using java.io.DataInputStream.readFully to read exactly 'length' bytes.
Declaration
Following is the declaration for java.io.ObjectInputStream.read(byte[] buf, int off, int len) method −
public int read(byte[] buf, int off, int len)
Parameters
buf − The buffer into which the data is read.
off − The start offset of the data.
len − The maximum number of bytes read.
Return Value
This method returns the actual number of bytes read, -1 is returned when the end of the stream is reached.
Exception
IOException − If an I/O error has occurred.
Example - Usage of ObjectInputStream read(byte[] buf, int off, int len) method
The following example shows the usage of Java ObjectInputStream read(byte[] buf, int off, int len) 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) { byte[] cbuf = new byte[10]; 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 into an array ois.read(cbuf, 0, 7); // print cbuf for (int i = 0; i < 7; i++) { System.out.print("" + (char) cbuf[i]); } } catch (Exception ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Hello
Example - Reading a Byte Array from a File
The following example shows the usage of Java ObjectInputStream read(byte[] buf, int off, int len) method. This example writes a byte array to a file and then reads it back into a buffer.
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.bin"); ObjectOutputStream oos = new ObjectOutputStream(fos); byte[] data = "Hello, World!".getBytes(); // Convert string to bytes oos.write(data); // Write byte array oos.close(); // Reading bytes using ObjectInputStream FileInputStream fis = new FileInputStream("data.bin"); ObjectInputStream ois = new ObjectInputStream(fis); byte[] buffer = new byte[20]; // Buffer to hold data int bytesRead = ois.read(buffer, 0, data.length); // Read into buffer System.out.println("Bytes Read: " + bytesRead); System.out.println("Read String: " + new String(buffer, 0, bytesRead)); // Convert bytes to string ois.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Bytes Read: 13 Read String: Hello, World!
Explanation
Writes the byte array "Hello, World!" to a file.
Reads the bytes into a buffer using read(byte[] buf, int off, int len).
Converts the buffer to a string and prints the content.
Example - Reading Data in Chunks
The following example shows the usage of Java ObjectInputStream read(byte[] buf, int off, int len) method. This example demonstrates reading data in chunks instead of all at once.
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("chunked_data.bin"); ObjectOutputStream oos = new ObjectOutputStream(fos); byte[] data = "Chunked Reading Example".getBytes(); // Convert string to bytes oos.write(data); // Write byte array oos.close(); // Reading bytes in chunks FileInputStream fis = new FileInputStream("chunked_data.bin"); ObjectInputStream ois = new ObjectInputStream(fis); byte[] buffer = new byte[8]; // Small buffer int bytesRead; System.out.println("Reading in chunks:"); while ((bytesRead = ois.read(buffer, 0, buffer.length)) != -1) { System.out.print(new String(buffer, 0, bytesRead)); // Convert to string and print } ois.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Reading in chunks: Chunked Reading Example
Explanation
Writes "Chunked Reading Example" to a file as raw bytes.
Reads 8 bytes at a time using read(byte[] buf, int off, int len).
Loops until all data is read, printing in chunks.