IO 入门级问题,谢谢

oicap1 2011-09-06 03:34:34
为什么我把 sd=(Student)ois.readObject(); 这段改成
Student sd2=(Student)ois.readObject();
然后下面打印的地方改成 sd2.age 就会报错。

import java.io.*;
class Serialization
{
public static void main(String[] args) throws IOException,ClassNotFoundException
{
Student sd=new Student(19,"dintdding",20,"computer");
FileOutputStream fos=new FileOutputStream("Myfile.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
try
{
oos.writeObject(sd);
oos.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
sd=null;
FileInputStream fis=new FileInputStream("Myfile.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
try
{
sd=(Student)ois.readObject();
fis.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
System.out.println("id = "+sd.id);
System.out.println("name = "+sd.name);
System.out.println("age = "+sd.age);
System.out.println("department = "+sd.department);
}
}

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 Student()
{
}
}


...全文
82 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
niuniu20008 2011-09-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 oicap1 的回复:]
多谢LS各位,确实是局部变量的问题。新手,前面没学好。
没意识到try代码块里的变量的作用域。
顺便问下,是不是一对大括号内定义的变量,作用域都在这对大括号内?
[/Quote]
是的。
brightyq 2011-09-06
  • 打赏
  • 举报
回复
嗯,LZ程序是try catch问题,不是IO问题。
oicap1 2011-09-06
  • 打赏
  • 举报
回复
多谢LS各位,确实是局部变量的问题。新手,前面没学好。
没意识到try代码块里的变量的作用域。
顺便问下,是不是一对大括号内定义的变量,作用域都在这对大括号内?
fengnenglu 2011-09-06
  • 打赏
  • 举报
回复
package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test3
{
public static void main(String[] args) throws IOException,ClassNotFoundException
{
Student sd=new Student(19,"dintdding",20,"computer");
Student sd2 = null;
FileOutputStream fos=new FileOutputStream("Myfile.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
try
{
oos.writeObject(sd);
oos.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
sd=null; FileInputStream fis=new FileInputStream("Myfile.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
try
{
sd2=(Student)ois.readObject();
fis.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
System.out.println("id = "+sd2.id);
System.out.println("name = "+sd2.name);
System.out.println("age = "+sd2.age);
System.out.println("department = "+sd2.department);
}
}

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 Student()
{
}
}
maen1987430 2011-09-06
  • 打赏
  • 举报
回复
局部变量问题。
fengnenglu 2011-09-06
  • 打赏
  • 举报
回复
你有一行把sd=null了,当然会报空指针异常啦!
maen1987430 2011-09-06
  • 打赏
  • 举报
回复
局部变量问题。
shuwei003 2011-09-06
  • 打赏
  • 举报
回复
因为你sd2是在try中定义的,如果出现异常,下面不知道sd2是什么东东。所以,如果用sd2,应该在try前面定义Student sd2

62,615

社区成员

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

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