Help: Text streams


Help is available for each task, or you can go straight to the solution source code.

Task 1

Create a PrintStream on file junk. Write the following out to a text file:
34
"an int is " + 34
3.14159
true

Open a generic FileOutputStream on junk and then attach a PrintStream to it. Use println for each output line.

Task 2

Close the file and open a FileInputStream to junk. Read the four lines back in.
Use a FileInputStream to open the file and a DataInputStream to read the lines back in. There is no analog to the PrintStream for input. Use the readLine method of DataInputStream instead.

Task 3

Verify that the four lines read in contain the correct values by first converting the strings (all but the third line) to primitive types. Print "ok" for all correct lines and "not ok" for all incorrect lines.
You will need to perform conversions from strings to primitives. See Integer, Float, and Boolean. Use methods such as valueOf and then convert the objects to their primitive equivalents.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.