import java.io.*; import java.util.*; public class ObjectToDisk { public static void main(String[] arguments) { Message mess = new Message(); String author = "Your son Scott"; String recipient = "Mom and Dad"; String[] letter = { "Hi, mom and dad!", "Things are going OK here at college. I could", "always use more money, but I don't blame you for", "sticking to our original agreement. If I eat less", "food at each meal, I can make it to the end of the", "term. I also can get a job -- my plan to give 8", "hours a week to save the spotted owl can wait", "another semester I guess. With any luck, there", "will be some owls left to save when that time", "comes. Gotta go. Feeling light-headed. Gave too", "much at the blood bank this morning. Thinking of", "you always." }; Date now = new Date(); mess.writeMessage(author, recipient, now, letter); try { FileOutputStream fo = new FileOutputStream( "Message.obj"); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(mess); oo.close(); System.out.println("Object created successfully."); } catch (IOException e) { System.out.println("Error -- " + e.toString()); } } } class Message implements Serializable { int lineCount; String from, to; Date when; String[] text; void writeMessage(String inFrom, String inTo, Date inWhen, String[] inText) { text = new String[inText.length]; for (int i = 0; i < inText.length; i++) text[i] = inText[i]; lineCount = inText.length; to = inTo; from = inFrom; when = inWhen; } }