参考两篇博客:
http://blog.csdn.net/moreevan/article/details/6697777
http://blog.csdn.net/moreevan/article/details/6698529
针对第二篇博客,看了下面的评论,发现子类输出的书号其实是父类的,书名是自己先添加的成员变量,我自己在实验的时候,发现子类NewBook中使用父类Book的变量部分,输出的都没有值:基本类型的系统默认值是0,非基础类型例如字符串系统默认是null
然后,当把父类Book实现了序列化接口时,这是才能全部输出子类的变量值:
并且第二篇博客里也写到了,如果在打印对象的时候,会调用对象的toString方法,即使我在子类中覆盖了toString()方法并且没有调用父类的toString方法,但是系统依然报出了第二篇博客中提到的那个错误:java.io.InvalidClassException: NewBook; no valid constructor
所以父类中加了个空的无参构造函数,这样就可以正常打印出来了,下面是全码:
1、父类没有实现序列化接口:(此时不可以打印子类的变量值)
importjava.io.Serializable;public classBook {intbook_num;
String book_authour;//构造函数
public Book(intabook_num, String abook_author)
{this.book_num =abook_num;this.book_authour =abook_author;
}publicBook()
{
}//设置作者名
public voidsetBookAuthor(String abook_author)
{this.book_authour =abook_author;
}//设置书号
public void setBookNum(intabook_num)
{this.book_num =abook_num;
}//打印对象
publicString toString(){return "book_num:"+book_num+"\n"+"book_anthor:"+book_authour;
}
}
2、父类实现序列化接口:(此时可以打印子类的变量值)
importjava.io.Serializable;public class Book implementsSerializable {intbook_num;
String book_authour;/*** 实现序列化接口的时候一定要分配序列ID*/
private static final long serialVersionUID = 1L;//构造函数
public Book(intabook_num, String abook_author)
{this.book_num =abook_num;this.book_authour =abook_author;
}publicBook(){
}//设置作者名
public voidsetBookAuthor(String abook_author)
{this.book_authour =abook_author;
}//设置书号
public void setBookNum(intabook_num)
{this.book_num =abook_num;
}//打印对象
publicString toString(){return "book_num:"+book_num+"\n"+"book_anthor:"+book_authour;
}
}
3、子类:
importjava.io.Serializable;public class NewBook extends Book implementsSerializable{privateString book_version;/*** 实现序列化接口的时候一定要分配序列ID*/
private static final long serialVersionUID = 1L;public NewBook(intabook_num, String abook_author,String abook_version)
{super(abook_num, abook_author);this.book_version =abook_version;//TODO Auto-generated constructor stub
}
@Overridepublic voidsetBookAuthor(String abook_author)
{//TODO Auto-generated method stub
super.setBookAuthor(abook_author);
}
@Overridepublic void setBookNum(intabook_num)
{//TODO Auto-generated method stub
super.setBookNum(abook_num);
}public voidsetBookVersion(String abook_version)
{
book_version=abook_version;
}
@OverridepublicString toString()
{//TODO Auto-generated method stub
return "new_book_num:"+book_num+"new_book_anthor:"+book_authour+"new_book_version"+book_version;
}
}
4、主函数:
importjava.io.EOFException;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutput;importjava.io.ObjectOutputStream;public classBookSerializable {public static voidmain(String args[])
{/** Java里的main方法里不能直接调用非静态的对象,要么调用静态变量,要么调用在方法内部实例化的非静态局部变量*/NewBook new_book1=new NewBook(1,"呼啸山庄","1.0");
NewBook new_book2=new NewBook(2,"红与黑","2.0");/** 将对象序列化到文件中*/
try{
File fil= new File("C://JavaFile//red_bean.txt");if(!fil.exists())
{
fil.createNewFile();
}
FileOutputStream fil_input= newFileOutputStream(fil);
ObjectOutputStream obj_output= newObjectOutputStream(fil_input);
obj_output.writeObject(new_book1);
obj_output.reset();
new_book1.setBookVersion("3.0");
obj_output.writeObject(new_book1);
obj_output.reset();
new_book1.setBookVersion("4.0");
obj_output.writeObject(new_book1);
obj_output.writeObject(new_book2);
obj_output.writeObject(null);//写入结束标志方便读取(非常重要,如果不写入,在读取的时候无法定位读取结束);
obj_output.close();//关闭对象输出流
fil_input.close();//关闭文件输出流
} catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}/** 将对象从文件中读出来*/ObjectInputStream obj_input;try{
FileInputStream fil_input= new FileInputStream("C://JavaFile//red_bean.txt");
obj_input= newObjectInputStream(fil_input);
Object boj;while((boj = obj_input.readObject())!=null)//循环读取对象流
{
NewBook new_bookReadTemp=(NewBook)boj;
System.out.println(new_bookReadTemp);/** 读出的时候按照写入的顺序读取*/
/*NewBook new_bookRead1 = (NewBook)obj_input.readObject();
NewBook new_bookRead2 = (NewBook)obj_input.readObject();
NewBook new_bookRead3 = (NewBook)obj_input.readObject();
NewBook new_bookRead4 = (NewBook)obj_input.readObject();
//NewBook new_bookRead5 = (NewBook)obj_input.readObject();
System.out.println(new_bookRead1);
System.out.println(new_bookRead2);
System.out.println(new_bookRead3);
System.out.println(new_bookRead4);
//System.out.println(new_bookRead5);*/}
obj_input.close();
fil_input.close();
}catch(EOFException e){
e.printStackTrace();
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(ClassNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
自己动手敲了一遍代码的感受:
确实应该多多动手呀!编惯了Android,感觉养成了很不好的语法习惯,main()是个静态方法,我还在main方法外面定义了了非静态对象,在main方法里用,显然不行啊,调用非静态方法,然后在非静态方法里使用非静态变量,或者直接在main方法里定义非静态局部变量并使用,Java里没有全局变量的概念,要共享数据就专门定义一个share类, 里面全是静态的变量方法。不要把安卓的onCreat和main混在一起。main里的变量定义和使用:http://blog.csdn.net/zi_jun/article/details/7553132
其他的Java序列化较好博客:
http://blog.csdn.net/wangzhiqing3/article/details/8392803
Java文件操作:
http://www.cnblogs.com/springcsc/archive/2009/12/03/1616367.html
http://blog.csdn.net/smartcat86/article/details/4085739/