package com.enterise.test;
import java.io.Serializable;
/**
* 实体实现序列化接口
* @author Always
*
*/
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age
* the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
package com.enterise.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
* 将对象保存在硬盘上--读取硬盘上的对象
* @author Always
*
*/
public class MainActivity {
public static void main(String[] args){
File child = new File("e://zhou");
if(!child.exists()){
child.mkdirs();
}
File parent = new File(child, "student.tmd");
Student stu = new Student("xingming",2);
//将对象保存到本地的硬盘
try {
FileOutputStream stream = new FileOutputStream(parent);
ObjectOutputStream outStream = new ObjectOutputStream(stream);
outStream.writeObject(stu);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//从硬盘中读取对象
try {
FileInputStream stream = new FileInputStream(parent);
ObjectInputStream inStream = new ObjectInputStream(stream);
int len = 0;
byte[] by = new byte[1024];
Student student = (Student) inStream.readObject();
System.out.println("studn------>"+student.getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}