知识点补充:
序列化: 将对象的信息以二进制的形式永久的保存在外部的介质当中。 这个过程就称之为对象的序列化。
序列化: ObjectOutputStream
对象的反序列化: 将外部介质当中的对象信息读取到内存当中,这一个过程就称之为对象的反序列化。
反序列化: ObjectInputStream :
1:序列化
ObjectOutputStream: 序列化:
构造器:
new ObjectOutputStream(OutputStream out);
API:
writeObject(Object obj);
注意: 要想序列化对象,对象必须要实现一个标志性接口》 Serializable接口。
标志性接口: 标识某个对象能够完成某些操作。
2: 反序列化:
ObjectInputStream(InputStream in);
API:
readObject();返回的是Object类型。 需要使用具体子类型的方法,需要强制准换。
总结:
(1)当类没有固定版本号码时候,类进行了修改, 反序列失败。
原因: 内存当中类的serialVersionUID 和文件当中的serialVersionUID 不一致,会触发异常:
补充:serialVersionUID 默认情况,给根据访问权限修饰符和 属性的个数决定。
解决: 固定类的版本号码:
private static final long serialVersionUID = 42L;
(2)static 修饰的属性不能被序列化。 反序列时,读取到的是null。
(3) 如果成员变量不想被序列化, 可以使用 transient 关键字进行修饰。
引用补充知识点 https://www.cnblogs.com/xdp-gacl/p/3777987.html
java.io.ObjectOutputStream代表对象输出流,它的writeObject(Object obj)方法可对参数指定的obj对象进行序列化,把得到的字节序列写到一个目标输出流中.java.io.ObjectInputStream
代表对象输入流,它的的readObject()方法从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回。
只有实现了序列化和外部化接口的类的对象才能被序列可扩展接口,实现可串行化接口,实现可控制序列化的行为,而仅实现序列化接口的类可以采用默认的序列化方式。
对象序列化包括如下步骤:
1)创建一个对象输出流,它可以包装一个其他类型的目标输出流,如文件输出流;
2)通过对象输出流的writeObject()方法写对象。
对象反序列化的步骤如下:
1)创建一个对象输入流,它可以包装一个其他类型的源输入流,如文件 输入流;
2)通过对象输入流的readObject()方法读取对象。
Java中Cloneable,Serializable接口详解
详情见 http://www.importnew.com/17964.html
实例序列化
import java.io.Serializable;
@SuppressWarnings("unused")
public class Student implements Serializable,Cloneable{
private static final long serivalversionUID=42L;
private String name;
// age 不可以被序列化
private transient int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name,int age)
{
this.name=name;
this.age=age;
}
public Student ()
{
}
public String toString()
{
return "name"+this.name+"age"+this.age;
}
public static long getSerivalversionuid() {
return serivalversionUID;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Objectoperator {
public Objectoperator(){
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
write2File();
readFromFile();
}
public static void readFromFile() throws FileNotFoundException, IOException, ClassNotFoundException
{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("E:/Workspaces/MyEclipse 10/yidong/src/面向对象/xuliehua.obj")));
// 需要强转成需要的类型
Student stu=(Student)ois.readObject();
System.out.println(stu);
ois.close();
}
public static void write2File() throws FileNotFoundException, IOException
{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("E:/Workspaces/MyEclipse 10/yidong/src/面向对象/xuliehua.obj")));
Student stu=new Student("lisi",23);
oos.writeObject(stu);
oos.close();
}
一开始没有为Student类实现implements Serializable接口
出现了异常加上之后可以正常运行
补充
Java矢量类
矢量类实现了一个动态数组和ArrayList中和相似,但是两者是不同的:
http://www.runoob.com/java/java-vector-class.html
本文用vevtor实现 http://www.runoob.com/java/java-enumeration-interface.html
package 面向对象;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
public class CombineText
{
public static void main(String[] args) throws IOException {
sequenceMethod02();
}
public static void sequenceMethod02() throws IOException
{
Vector v=new Vector();
FileInputStream in1=new FileInputStream( "E:/Workspaces/MyEclipse 10/yidong/src/面向对象/a.txt");
FileInputStream in2=new FileInputStream( "E:/Workspaces/MyEclipse 10/yidong/src/面向对象/b.txt");
v.add(in1);
v.add(in2);
Enumeration elements=v.elements();
SequenceInputStream sis=new SequenceInputStream(elements);
FileOutputStream fos =new FileOutputStream( "E:/Workspaces/MyEclipse 10/yidong/src/面向对象/pipiwen.txt");
byte[] buf=new byte[1024];
int num;
while((num=sis.read(buf))!=-1)
{
fos.write(buf,0,num);
fos.close();
}
System.out.println("successsful");
fos.close();
sis.close();
}
// public static void sequenceMethod01() throws IOException
// {
// FileInputStream in1=new FileInputStream( "E:/Workspaces/MyEclipse 10/yidong/src/面向对象/a.txt");
// FileInputStream in2=new FileInputStream( "E:/Workspaces/MyEclipse 10/yidong/src/面向对象/b.txt");
// SequenceInputStream sis=new SequenceInputStream(in1,in2);
// FileOutputStream fos=new FileOutputStream("E:/Workspaces/MyEclipse 10/yidong/src/面向对象/pipiwen.txt");
// byte[] buf=new byte[1024];
// int num;
// while((num=sis.read(buf))!=-1)
// {
// fos.write(buf,0,num);
// fos.flush();
// }
// System.out.println("successful");
// fos.close();
// sis.close();
//
//
// }
}
两种方法实现文件合并
2018年8月9日11点50分36秒于易动
今天早上坐火车刚回到学校,还是卧铺舒服哈哈哈哈
加油学习啦~~~