try (InputStream fis = new FileInputStream("file.bin"); ObjectInputStream ois = new ObjectInputStream(fis)) { MyObject obj = (MyObject) ois.readObject(); System.out.println(obj.toString()); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); }
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais)) { MyObject obj = (MyObject) ois.readObject(); System.out.println(obj.toString()); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); }In this example, we create a ByteArrayInputStream to read from a byte array called "bytes", and we create an ObjectInputStream to read objects from that input stream. We then read an object of type MyObject from the ObjectInputStream and print its toString() representation. As mentioned earlier, ObjectInputStream is part of the java.io package, which is included in the Java standard library.