定义一个类,继承Serializable。该类的实例将被写到文件,并被读出还原。
Junk.java
/*
* Junk.java
*
* Created on 2007?¨º3??3¨¨?, ????11:24
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package serializableapp;
import java.io.Serializable;
public class Junk implements Serializable {
private static java.util.Random generator = new java.util.Random();
private int answer; // The answer
private double[] numbers; // Valuable data
private String thought; // A unique thought
public Junk(String thought) {
this.thought = thought;
answer = 42; // Answer always 42
numbers = new double[3+generator.nextInt(4)]; // Array size 3 to 6
for(int i = 0 ; i<numbers.length ; i++) { // Populate with
numbers[i] = generator.nextDouble(); // random values
}
}
public String toString() {
StringBuffer strBuf = new StringBuffer(thought);
strBuf.append('\n').append(String.valueOf(answer));
for(int i = 0 ; i<numbers.length ; i++) {
strBuf.append("\nnumbers[")
.append(String.valueOf(i))
.append("] = ")
.append(numbers[i]);
}
return strBuf.toString();
}
}
执行Serialize的程序
SerializableApp/serializableapp/Junk.java
SerializableApp/serializableapp/SerializeObjects.java
/*
* SerializeObjects.java
*
* Created on 2007?¨º3??3¨¨?, ????11:24
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package serializableapp;
//import java.io.ObjectOutputStream;
//import java.io.BufferedOutputStream;
//import java.io.FileOutputStream;
//import java.io.IOException;
import java.io.*;
public class SerializeObjects {
public static void main(String[] args) {
Junk obj1 = new Junk("A green twig is easily bent.");
Junk obj2 = new Junk("A little knowledge is a dangerous thing.");
Junk obj3 = new Junk("Flies light on lean horses.");
ObjectOutputStream objectOut = null;
try {
// Create the object output stream
objectOut = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream("C:/Beg Java Stuff/JunkObjects.bin")));
// Write three objects to the file
objectOut.writeObject(obj1); // Write object
objectOut.writeObject(obj2); // Write object
objectOut.writeObject(obj3); // Write object
/*
System.out.println("\n\nobj1:\n" + obj1
+"\n\nobj2:\n" + obj2
+"\n\nobj3:\n" + obj3);
**/
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
// Close the stream
try {
objectOut.close(); // Close the output stream
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
ObjectInputStream objectIn = null; // Stores the stream reference
int objectCount = 0; // Number of objects read
Junk object = null; // Stores an object reference
try {
objectIn = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("C:/Beg Java Stuff/JunkObjects.bin")));
// Read from the stream until we hit the end
while(true) {
object = (Junk)objectIn.readObject();// Read an object
objectCount++; // Increment the count
System.out.println(object); // Output the object
}
} catch(ClassNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
} catch(EOFException e) { // This will execute when we reach EOF
System.out.println("EOF reached. "+ objectCount + " objects read.");
} catch(IOException e) { // This is for other I/O errors
e.printStackTrace(System.err);
System.exit(1);
}
// Close the stream
try {
objectIn.close(); // Close the input stream
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
执行Deserialize的程序
Deserialize/serializableapp/Junk.java
Deserialize/deserialize/DeserializeObjects.java
/*
* DeserializeObjects.java
*
* Created on 2007?¨º3??4¨¨?, ¨¦???9:34
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package deserialize;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.EOFException;
class DeserializeObjects {
public static void main(String args[]) {
ObjectInputStream objectIn = null; // Stores the stream reference
int objectCount = 0; // Number of objects read
serializableapp.Junk object = null; // Stores an object reference
try {
objectIn = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("C:/Beg Java Stuff/JunkObjects.bin")));
// Read from the stream until we hit the end
while(true) {
object = (serializableapp.Junk)objectIn.readObject();// Read an object
objectCount++; // Increment the count
System.out.println(object); // Output the object
}
} catch(ClassNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
} catch(EOFException e) { // This will execute when we reach EOF
System.out.println("EOF reached. "+ objectCount + " objects read.");
} catch(IOException e) { // This is for other I/O errors
e.printStackTrace(System.err);
System.exit(1);
}
// Close the stream
try {
objectIn.close(); // Close the input stream
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
没有评论:
发表评论