java对象转换的问题

zs312979674 2011-09-21 04:28:54

package test;
import java.io.*;

public class SerializableTest {

public static void main(String[] args){

/*
*
* 序列化
*/
FileOutputStream fos = null;
ObjectOutputStream oos = null;
Person person = new Person("小明",32);
person.setSpouse("春花");

try {

fos = new FileOutputStream("d://text.person");
oos = new ObjectOutputStream(fos);
oos.writeObject(person);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

}

/*
*
* 反序列化
*/
Person p = null;
try {
FileInputStream fis = new FileInputStream("d://test.person");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();

if(obj instanceof Person)
/*
* //这里的变量p为什么要在上面Person p = null定义,
* 直接在这里Person p = (Person)obj要报错,是什么原因啊???
*/
p = (Person)obj;

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


/*
* 可序列化的类
*/
class Person implements java.io.Serializable{


/**
*
*/
private static final long serialVersionUID = 1L;

private String name;
private int age;
private String spouse;
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 String getSpouse() {
return spouse;
}
public void setSpouse(String spouse) {
this.spouse = spouse;
}


public Person(){}

public Person(String name,int age){}
}

...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhu6100441 2011-09-21
  • 打赏
  • 举报
回复
看错误信息就能看出来了,
FileInputStream fis = new FileInputStream("d:\\text.person");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();

由于路径写错,所以你的obj根本就读不出个东西来,所以你在转换的时候,也会报错。
原来缘来 2011-09-21
  • 打赏
  • 举报
回复

package test;
import java.io.*;

public class SerializableTest {

public static void main(String[] args){

/*
*
* 序列化
*/
FileOutputStream fos = null;
ObjectOutputStream oos = null;
Person person = new Person("小明",32);
person.setSpouse("春花");

try {

fos = new FileOutputStream("d:\\text.person");
oos = new ObjectOutputStream(fos);
oos.writeObject(person);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

}

/*
*
* 反序列化
*/

try {
FileInputStream fis = new FileInputStream("d:\\text.person");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();

Person p =(Person) obj;
/*
* //这里的变量p为什么要在上面Person p = null定义,
* 直接在这里Person p = (Person)obj要报错,是什么原因啊???
*/
System.out.println(p.getSpouse());

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


/*
* 可序列化的类
*/
class Person implements java.io.Serializable{


/**
*
*/
private static final long serialVersionUID = 1L;

private String name;
private int age;
private String spouse;
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 String getSpouse() {
return spouse;
}
public void setSpouse(String spouse) {
this.spouse = spouse;
}


public Person(){}

public Person(String name,int age){}
}


除了路径拼写错误,没有什么错误呀,楼主说得我也试了,没问题呀
风尘中国 2011-09-21
  • 打赏
  • 举报
回复
你序列化的时候d:\\test.person 你拼错了,还有一些问题,给你贴出代码给你看下

import java.io.*;

public class SerializableTest {

public static void main(String[] args){

/*
*
* 序列化
*/
FileOutputStream fos = null;
ObjectOutputStream oos = null;
Person person = new Person("小明",32);
person.setSpouse("春花");

try {

fos = new FileOutputStream("d://test.person");
oos = new ObjectOutputStream(fos);
oos.writeObject(person);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

}

/*
*
* 反序列化
*/
Person p = null;
try {
FileInputStream fis = new FileInputStream("d://test.person");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();

if(obj instanceof Person)
/*
* //这里的变量p为什么要在上面Person p = null定义,
* Person p = null根本无所谓
* 直接在这里Person p = (Person)obj要报错,是什么原因啊???
* 直接这样转换也不会报错
*/
p = (Person)obj;
System.out.println(p);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


/*
* 可序列化的类
*/
class Person implements java.io.Serializable{


/**
*
*/
private static final long serialVersionUID = 1L;

private String name;
private int age;
private String spouse;

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", spouse='" + spouse + '\'' +
'}';
}

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 String getSpouse() {
return spouse;
}
public void setSpouse(String spouse) {
this.spouse = spouse;
}


public Person(){}

public Person(String name,int age){
this.name=name;
this.age=age;
}
}

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧