为什么我把 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()
{
}
}
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()
{
}
}
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()
{
}
}
没意识到try代码块里的变量的作用域。
顺便问下,是不是一对大括号内定义的变量,作用域都在这对大括号内?