java_可持续性,把两个程序改成一个程序

import java.io.*;
class Student implements Serializable{
int id;
String name;
int age;
String department;
public Student(int id,String name,int age,String department){
this.id=id;
this.name=name;
this.age=age;
this.department=department;
}
}

public class Objectser implements Serializable{
public static void main(String args[]){
Student stu=new Student(703034138,"sunwxb",23,"CP");
try{
FileOutputStream fo=new FileOutputStream("data.ser");
ObjectOutputStream so=new ObjectOutputStream(fo);
so.writeObject(stu);
so.close();
}catch(Exception e){
System.out.println(e);
}
}
}

import java.io.*;
class Student implements Serializable{
int id;
String name;
int age;
String department;
public Student(int id,String name,int age,String department){
this.id=id;
this.name=name;
this.age=age;
this.department=department;
}
}
public class ObjectRecov Serializable{
public static void main(String args[]) {
Student stu;
try{
FileInputStream fi=new FileInputStream("data.ser");
ObjectInputStream si=new ObjectInputStream(fi);
stu=(Student)si.readObject();
si.close();
}catch(Exception e){
System.out.println(e);
}

System.out.println("ID:" +stu.id +"name:" +stu.name +"age" +stu.age +"dept:" +stu.department);
}
}

第1个回答  推荐于2016-07-23
package com.lili.test;

import java.io.*;

/**
* 对象序列化 转换器
*
* @author: 李立
* @date : Nov 2, 2009 4:36:50 PM
*/
public class JavaObjectSerializator {

public static void main(String args[]) {
String path = "F:\\文件\\";
String file = "object.txt";
Student s=new Student();
s.setAge(26);
s.setName("lili");
s.setDepartment("计算机");
s.setId(1);

try {
System.out.println(s);
//写入对象
JavaObjectSerializator.serializateObjectToFile(s, path+file);
//读出对
Student st=(Student)JavaObjectSerializator.readObjectFromFile(path+file);

System.out.println(st);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* 序列化 对象 到 文件
* @param o
* @param file
* @throws IOException
*/
public static void serializateObjectToFile(Object o,String file) throws IOException{
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(o);
oos.close();
}
/**
* 反序列化 从文件中 读取 对象
* @param file
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object readObjectFromFile(String file) throws IOException, ClassNotFoundException{
Object o=null;
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
o=ois.readObject();
return o;
}
}

class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;

int id;

String name;

int age;

String department;

public Student(){

}

public Student(int id, String name, int age, String department) {
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {

return "{姓名:"+this.getName()+" ,院系:"+this.getDepartment()+" ,年龄:"+this.getAge()+"}";
}

}本回答被提问者采纳
第2个回答  2009-10-31
不懂什么意思,读写分别做个方法 是这意思吧
第3个回答  2009-10-31
这个事啥玩意啊??
相似回答