Java IO Tutorial - Java ObjectInputStream.read(byte[] buf, int off, int len)








Syntax

ObjectInputStream.read(byte[] buf, int off, int len) has the following syntax.

public int read(byte[] buf, int off, int len)  throws IOException

Example

In the following code shows how to use ObjectInputStream.read(byte[] buf, int off, int len) method.

/*from  w ww.  j a  v a  2  s .co  m*/

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

public class Main {

  public static void main(String[] args) throws Exception {

    byte[] cbuf = new byte[10];

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    oout.writeUTF("Hello World from java2s.com");
    oout.flush();
    oout.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
        "test.txt"));

    ois.read(cbuf, 0, 7);

    for (int i = 0; i < 7; i++) {
      System.out.print((char) cbuf[i]);
    }
    ois.close();
  }
}

The code above generates the following result.