Java Reference
In-Depth Information
catch
(IOException e)
{
System.out.println(
"Error reading from keyboard.");
}
}
}
28.
ObjectOutputStream outputThisWay =
new
ObjectOutputStream(
new
FileOutputStream("bindata.dat"));
29.
outputThisWay.writeDouble(v1);
outputThisWay.writeDouble(v2);
30.
outputThisWay.writeUTF("Hello");
31.
outputThisWay.close();
32.
ObjectInputStream inputThing
=
new
ObjectInputStream(
new
FileInputStream("someStuff"));
33.
number
=
inputThing.readDouble();
34.
inputThing.close();
35. If a number is written to a file with
writeInt
, it should be read only with
readInt
.
If you use
readLong
or
readDouble
to read the number, something will go wrong.
36. You should not use
readUTF
to read a string from a text file. You should use
readUTF
only to read a string from a binary file. Moreover, the string should have
been written to that file using
writeUTF
.
37. When opening a binary file for either output or input in the ways discussed in this
chapter, a
FileNotFoundException
might be thrown and other
IOException
s
may be thrown.
38. An
EOFException
is thrown when your program tries to read the (nonexisting)
fourth number.
39. It is not an infinite loop because when the end of the file is reached, an exception
will be thrown, and that will end the entire
try
block.
40. You add the two words
implements
Serializable
to the beginning of the class
definition. You also must do this for the classes for the instance variables and so on
for all levels of class instance variables within classes.
41.
import
java.io.Serializable;
or
import
java.io.*;
42. The return type is
Object
, which means the returned value usually needs to be
type cast.