Other types of persistence
You have seen how object serialization provides a relatively straightforward mechanism for saving the contents of one or more objects to a disk file. This section briefly mentions a few other ways in which data of various sorts can be saved to files.
Saving individual attribute values
The DataOutputStream class provides a wrapper that lets you save individual attribute values through the following methods:
writeBoolean(boolean b); writeByte(int v); writeChar(int c); writeChars(String s); writeDouble(double v); writeFloat(Float v); writeInt(int v); writeLong(long v); writeShort(short v);
An example of how you might use this class follows:
File myStream = new File("mystream.dat");
DataOutputStream myDataOutputStream = null;
try {
myDataOutputStream = new DataOutputStream
(new BufferedOutputStream
(new FileOutputStream(myStream)));
myDataOutputStream.writeInt(50);
myDataOutputStream.writeBoolean(true);
myDataOutputStream.flush();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
myDataOutputStream.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
- The above wraps a
FileOutputStreaminside aBufferedOutputStream, which is in turn wrapped inside aDataOutputStream - It then invokes the
writeInt()andwriteBoolean()methods with the values50andtruerespectively - The
flush()method forces the data to be written to the file, and the file is closed[1] inside thefinallyblock
The DataInputStream class provides a wrapper with "read" methods corresponding to the DataOutputStream class's "write" methods. Here is some code that can read back the int and boolean values which were saved to the above file:
File myStream = new File("mystream.dat");
DataInputStream myDataInputStream = null;
try {
myDataInputStream = new DataInputStream
(new BufferedInputStream
(new FileInputStream(myStream)));
int i = myDataInputStream.readInt();
boolean b = myDataInputStream.readBoolean();
System.out.println("i=" + i + ", b=" + b);
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
myDataInputStream.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
Note how the above used the readInt() and readBoolean() methods to obtain the data from the file. You need to specify these in the same order they were written to the file because the read methods effectively position to the next value each time.
Saving text files
So far in this section you have made use of streams, which you can think of as the mechanism for transmitting a series of bytes. For textual files Java provides the Writer class (and its subclasses) for when you want to store character-based files, and the Reader class (and its subclasses) to read them back again.
Here is an example that uses a FileWriter wrapped inside a BufferedWriter to save a file consisting of a well known piece of prose:
File myFile = new File("myfile.txt");
BufferedWriter myBufferedWriter = null;
try {
myBufferedWriter = new BufferedWriter(new FileWriter(myFile));
myBufferedWriter.write("To be, or not to be.");
myBufferedWriter.newLine();
myBufferedWriter.write("That is the question.");
myBufferedWriter.flush();
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
myBufferedWriter.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
- The
write()method takes aStringobject and thenewLine()method causes a line separator to appended, so that the next call towrite()will cause its piece of text to appear on the next line in the file
If you run the above you can see and open the created file by clicking on the Files window in NetBeans (immediately to the right of Projects) and look for the file named myfile.txt. This should display the following:
To be, or not to be. That is the question.
An alternative class that can be used for writing text files is PrintWriter, which has the method println() to save a String with an automatic line separator.
There follows some code that will read the file back from the disk and send each line to the Output window:
File myFile = new File("myfile.txt");
BufferedReader myBufferedReader = null;
String line = null;
try {
myBufferedReader = new BufferedReader(new FileReader(myFile));
line = myBufferedReader.readLine();
while (line != null) {
System.out.println(line);
line = myBufferedReader.readLine();
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
try {
myBufferedReader.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
- Note the use of the
whileloop so that as many lines will be read as appear in the file. Prior to the while loop the first line is read, and provided it is notnull(which would indicate the end of the file has been reached) the loop is started - Inside the loop the current line is sent to the Output window and then the next line is read from the file. The loop continues until
nullis returned
Comments