Chapter 5. Persisting Your Application Using Files

The previous chapter discussed building user interfaces and stand-alone Java applications using the Java Foundation Classes (JFC). A key feature of many applications is the ability to save its state off to a file. Image manipulation programs need to read and write images to disk. Word processing and other office-productivity applications need to read and write spreadsheets, presentations, and text-based documents. Essentially, to do any of these save operations, an application must take its in-memory representation of its state, and write it to disk. Later on, this file can be read back into memory, putting the application back to exactly where the user had left using it.

Different applications need to save different pieces of information to disk. Some applications really only need to save their configuration to disk, because they may save their other data to a database (the subsequent chapter shows you how to persist your application's data to a database). A typical single-user application such as a word processor or image manipulation program will need to save its state to files. Java provides a couple of built-in mechanisms for saving or serializing data to disk. The two major APIs in the JDK for persisting application data to disk are the Java Serialization API for generic serialization and the XMLEncoder/Decoder API for serializing JavaBean components.

This chapter looks at the Java Serialization API, the XMLEncoder/Decoder API, ...

Get Professional Java® JDK®, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.