public interface ObjectOutput extends DataOutput ObjectOutput extends the DataOutput interface to include writing of objects. DataOutput includes methods for output of primitive types, ObjectOutput extends that interface to include objects, arrays, and Strings. 这个比较合适吧
/*TestBean.java(要保存的对象)*/ package km3;import java.io.Serializable;public class TestBean implements Serializable{
private String name; private String title; private String content; /** * @return 返回 content。 */ public String getContent() { return content; } /** * @param content 要设置的 content。 */ public void setContent(String content) { this.content = content; } /** * @return 返回 name。 */ public String getName() { return name; } /** * @param name 要设置的 name。 */ public void setName(String name) { this.name = name; } /** * @return 返回 title。 */ public String getTitle() { return title; } /** * @param title 要设置的 title。 */ public void setTitle(String title) { this.title = title; } } /*Test.java保存到磁盘上的过程*/ package km3;import java.io.File; import java.io.FileOutputStream; import java.io.ObjectOutputStream;public class Test { public static void main(String[] args) { try { //新建一个对象并将里面的属性付值 TestBean bean = new TestBean(); bean.setContent("ajkhakshfiuahf"); bean.setName("alkjiouoieruy"); bean.setTitle("ashiojroeiyioj"); //确定要保存在磁盘上的位置 File f = new File("e:/javaprogs/myFile.test"); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(f)); out.writeObject(bean); out.close(); System.out.println("save"); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } } /*Testin.java读取对象的过程*/ package km3;import java.io.File; import java.io.FileInputStream; import java.io.ObjectInputStream;public class Testin { public static void main(String[] args) { try { //确定要读取磁盘上的对象的文件 File f = new File("e:/javaprogs/myFile.test"); ObjectInputStream in = new ObjectInputStream(new FileInputStream(f)); TestBean bean = (TestBean)in.readObject(); System.out.println(bean.getContent()); System.out.println(bean.getName()); System.out.println(bean.getTitle()); System.out.println("load"); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } }
extends DataOutput
ObjectOutput extends the DataOutput interface to include writing of objects. DataOutput includes methods for output of primitive types, ObjectOutput extends that interface to include objects, arrays, and Strings. 这个比较合适吧
package km3;import java.io.Serializable;public class TestBean implements Serializable{
private String name; private String title; private String content;
/**
* @return 返回 content。
*/
public String getContent() {
return content;
}
/**
* @param content 要设置的 content。
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return 返回 name。
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name。
*/
public void setName(String name) {
this.name = name;
}
/**
* @return 返回 title。
*/
public String getTitle() {
return title;
}
/**
* @param title 要设置的 title。
*/
public void setTitle(String title) {
this.title = title;
}
}
/*Test.java保存到磁盘上的过程*/
package km3;import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;public class Test { public static void main(String[] args) {
try {
//新建一个对象并将里面的属性付值
TestBean bean = new TestBean();
bean.setContent("ajkhakshfiuahf");
bean.setName("alkjiouoieruy");
bean.setTitle("ashiojroeiyioj");
//确定要保存在磁盘上的位置
File f = new File("e:/javaprogs/myFile.test");
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(f));
out.writeObject(bean);
out.close();
System.out.println("save");
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
/*Testin.java读取对象的过程*/
package km3;import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;public class Testin {
public static void main(String[] args) {
try {
//确定要读取磁盘上的对象的文件
File f = new File("e:/javaprogs/myFile.test");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
TestBean bean = (TestBean)in.readObject();
System.out.println(bean.getContent());
System.out.println(bean.getName());
System.out.println(bean.getTitle());
System.out.println("load");
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
implements Serializabel这个接口.
使用缺省的serializetion的实现时,一个ObjectOutputStream的构造和一个ObjectInputStream的构造必须一一对应.ObjectOutputStream的构造函数会向输出流中写入一个标识头,而ObjectInputStream会首先读入这个标识头.因此,多次以追加方式向一个文件中写入object时,该文件将会包含多个标识头.所以用ObjectInputStream来deserialize这个ObjectOutputStream时,将产生StreamCorruptedException.一种解决方法是可以构造一个ObjectOutputStream的子类,并覆盖writeStreamHeader()方法.被覆盖后的writeStreamHeader()方法应判断是否为首次向文件中写入object,羰?则调用super.writeStreamHeader();若否,即以追加方式写入object时,则应调用ObjectOutputStream.reset()方法.
如何实现呢?是不是写一个类继承ObjectOutputStream,假设名为ExdObjectOutputStream,在这个类中覆盖writeStreamHeader()方法:
protected void writeStreamHeader() throws IOException
{if (){super.writeStreamHeader();}
else{ObjectOutputStream.reset()}
}if条件表达式如何写?如何判断文件为空?(这个ObjectOutputStream就可以用super代替?)
然后在程序中不用new ObjectOutputStream(f);而是用new ExdObjectOutputStream(f);?此外,如何判断到达这样的一个文件的尾部?
感谢各位回贴。