Java Reference
In-Depth Information
FileOutputStream fileOut = new FileOutputStream("bank.ser");
⇒
Creates a
file output stream from a file.
ObjectOutputStream out = new ObjectOutputStream(fileOut);
⇒
Creates an
object output stream for the file.
out.writeObject(people);
⇒
Writes the
people
object to the file.
⇒
Writes the double
d
to the binary file.
out.writeDouble(d);
out.writeInt(n);
⇒
Writes the integer
n
to the binary file.
⇒
Writes the string
s
to the binary file.
out.writeChars(s);
⇒
Writes the Boolean value
b
to the binary file.
out.writeBoolean(b);
out.close();
⇒
Closes the object output stream.
fileOut.close();
⇒
Closes the file.
FileInputStream fileIn = new FileInputStream("bank.ser");
⇒
Creates a file
input stream from a file.
⇒
Creates an object
ObjectInputStream in = new ObjectInputStream(fileIn);
input stream from a file.
person = (Person) in.readObject();
⇒
Reads an object of type
Person
from the
file.
⇒
Reads a double from a binary file.
double d=in.readDouble();
int n=in.readInt();
⇒
Reads an integer from a binary file.
String s=in.readChars();
⇒
Reads a string from a binary file.
⇒
boolean b=in.readBoolean();
Reads a Boolean value from a binary file.
in.close();
⇒
Closes the object output stream.
⇒
Closes the file output stream.
fileOut.close();
13.6 Important Points
1. There are two types of exceptions:
checked
and
unchecked
. Java forces us to handle
checked exceptions. Conversely, unchecked exceptions do not have to be handled, but
can be. If an exception is generated but not handled, then the program crashes.
2. When handling an exception in the
catch
blocks, always start with the most specific
exceptions and then continue with the more general exceptions. If this rule is not
followed, then the later
catch
blocks could never be executed and a syntax error will
be generated.
3. A
finally
block is always executed. Use a
finally
block to close files and free
resources.