Don't Cross the Streams
Summary
This exercise will introduce you to reading data from a binary file. You will create a program that can read to and write from a
Note: This method of storing data in a text file is known as CSV or comma-separated values (a.k.a. comma delimited) and is still in common use.
Task
- Design and implement a class which has four properties, of types int, string, float and bool.
You can choose the names.
- Add a method that accepts a StreamWriter parameter and writes the state of the object to that
stream.
- Add a method that accepts a StreamReader parameter and can read the state of the object from that
stream.
-
Write a test program to
- Create a new instance of your class
- Set its values
- Open a binary file and save the state of the object to it
- Close the file
- Clear the object reference variable by setting it to null
- Open the file to read from it
- Create a new instance of your class and read the state out of the file.
- Close the file
- Make sure to include exception handling for the file access.
- Check that the data read in is correct.
Questions
- Experiment by changing the order in which variables are read from the stream when reading. Does it work?
- How would you use the same file to store data from instances of two different classes?