Java Reference
In-Depth Information
iif ( inputFile . exists () && ! inputFile . renameTo ( backupFile )) {
throw
throw new
new IOException ( "Could not rename file to backup file" );
}
// Rename the temporary file to the save file.
iif (! tmpFile . renameTo ( inputFile )) {
throw
throw new
new IOException ( "Could not rename temp file to save file" );
}
state = State . AVAILABLE ;
}
}
See Also
The Preferences API (see Storing Strings in Properties and Preferences ) allows you to save
small amounts of preference data, but is not convenient for saving program state. The Object
Serialization API (see Saving and Restoring Java Objects ) will serialize objects to/from an
external representation. To serialize using XML, consider using JAXB (see Converting
Between Objects and XML with JAXB ) or the XML Serializers (see Converting Between
Objects and XML with Serializers ) . You could also save it in JSON (see Chapter 19 ). The
XML and JSON forms have the benefit that they store text files (somewhat larger, but more
portable).
Acknowledgements
The code in this program is my own, based on my experience in various applications. I was
prompted to package it up this way, and write it up, by a posting made by Brendon McLean
to the mailing list for the now-defunct Java Application Framework JSR-296 .
Program: Text to PostScript
There are several approaches to printing in Java. In a GUI application, or if you want to use
the graphical facilities that Java offers (fonts, colors, drawing primitives, and the like), you
should refer to Printing in Java . However, sometimes you simply want to convert text into a
form that prints nicely on a printer that isn't capable of handling raw text on its own (such as
many of the PostScript devices on the market). The program in Example 10-14 shows code
for reading one or more text files and outputting each of them in a plain font with PostScript
around it. Because of the nature of PostScript, certain characters must be escaped; this is
handled in toPsString() , which in turn is called from doLine() . There is also code for
Search WWH ::




Custom Search