-2

I have to create a main program that will do the following. Create a single object of the class type that you added to your project. Write that single object to the binary file. After the data has been written, open the file to read and load the data back in. Print the data that was read from the file to demonstrate that the file can be read.

Is there anyone that is able to help?

  • 1
    I'm guessing the task refers to the Serializable API, which is horrible, and badly documented. This is the starting point: https://docs.oracle.com/javase/8/docs/technotes/guides/serialization/index.html – Sean Patrick Floyd Apr 16 '18 at 23:44

1 Answers1

0

I'm guessing the task is referring to the Serializable API. If so, there's a few things you need to do.

  1. Make your custom object implement Serializable
  2. write the file by wrapping a FileOutputStream in an ObjectOutputStream to which you add the object
  3. read the Object from an ObjectInputStream with a wrapped FileInputStream

Look at this example for guidance, but replace Socket streams with FileInput/OutputStreams.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588