FileInputStream fileInputStream = new FileInputStream("object.dat"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); try { Object obj = objectInputStream.readObject(); System.out.println("Read object: " + obj); } catch (ClassNotFoundException e) { e.printStackTrace(); } objectInputStream.close();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); try { Object obj = objectInputStream.readObject(); System.out.println("Read object: " + obj); } catch (ClassNotFoundException e) { e.printStackTrace(); } objectInputStream.close();This code creates a ByteArrayInputStream and ObjectInputStream for reading objects from a byte array. It then uses the readObject() method to read an object from the input stream, and prints it to the console. The java.io.ObjectInputStream class is part of the Java standard library and belongs to the java.io package.