现在要做一个能简单实现学生信息录入和查询的程序,用IO中的输入\输出对象到二进制文件实现,现在存在一个问题,就是第一次查询没有问题,但进行第二次查询时会抛出IOException,请问如何解决,十分感谢。初学JAVA,大家不要见笑。下面是源代码,其中query是查询方法:import java.io.*;
public class Student implements Serializable{

private int number;
private String name;
private char sex;
private int[] performance;
private double average;


public Student()
{
number=0;
name=null;
sex='M';
performance=null;
average=0;
}
public static void choose()
{
char choose='a';
while(choose!='1'&&choose!='2')
{
System.out.println("录入数据请按1,查询请按2,退出请按其他任意键");
choose=SavitchIn.readLineNonwhiteChar();
}
if(choose=='1')
type();
else if(choose=='2')
query();
else
System.exit(0);
}
public void writeIn()
{
System.out.println("请输入学号");
number=SavitchIn.readLineInt();
System.out.println("请输入姓名");
name=SavitchIn.readLine();
System.out.println("请输入性别");
sex=SavitchIn.readLineNonwhiteChar();
System.out.println("请分别输入10门课的成绩");
performance=new int[10];
for(int j=0;j<10;j++)
{
System.out.println("请输入第"+(j+1)+"门课的成绩");
performance[j]=SavitchIn.readLineInt();
}
average();

}

private void average()
{
int sum=0;
for(int i=0;i<10;i++)
{
sum=sum+performance[i];

}
average=sum/10;
}
public void write()
{
System.out.println("学号: "+number);
System.out.println("姓名: "+name);
System.out.println("性别: "+sex);
for(int i=1;i<10;i++)
System.out.println("第"+(i+1)+"门的成绩为: "+performance[i]);
System.out.println("平均成绩: "+average);

}
public static void query()
{
int number=0;
boolean goon=true;
char escape='a';
Student student=new Student();
ObjectInputStream inputStream=null;
try
{
inputStream=new ObjectInputStream(new FileInputStream("student.dat"));
}
catch(IOException e)
{
System.out.println("读取文件出现错误");
System.exit(0);
}
while(goon==true)
{
System.out.println("请输入需要查询的学号");
number=SavitchIn.readLineInt();

try{
while(true)
{
student=(Student)inputStream.readObject();
if(student.number==number)
{
student.write();
inputStream.close();
break;
}
}
}

            catch(EOFException e)
            {
             System.out.println("您输入的学号没有记录");
            }
catch(IOException e)
{
System.out.println("读取文件发生错误");
System.exit(0);

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


System.out.println("请按Q键退出,其他任意键继续查询");
escape=SavitchIn.readLineNonwhiteChar();
if(escape=='q'||escape=='Q')
{
goon=false;

}
}
choose();



}
public static void type ()
{

char a='a';
ObjectOutputStream outputStream=null;
try{
outputStream=new ObjectOutputStream(new FileOutputStream("student.dat"));
}
catch(IOException e)
{
System.out.println("不能创建或更改指定文件");
System.exit(0);
}
while(!(a=='q'||a=='Q'))
{
Student student=new Student();
student.writeIn();
try{
outputStream.writeObject(student);
}
catch(IOException e)
{
System.out.println("不能向文件写入");
System.exit(0);
}
System.out.println("结束录入请按Q,按其他任意键继续输入");
a=SavitchIn.readLineNonwhiteChar();
}
try{
outputStream.close();
}
catch(IOException e)
{
System.out.println("关闭文件时出现错误");
System.exit(0);
}

choose();
}
public static void main(String[] args)
{
choose();
}

}

解决方案 »

  1.   

    我们这本教材为了减轻学生负担,自带的一个键盘输入函数,SavitchIn.readLineInt()就是从键盘读取一个整数,SavitchIn.readLineNonwhiteChar是读取一个非空白字符
      

  2.   

    我找到了,在query方法中,第一次执行时,inputStream.close(); 将文件关闭了,如果用户没有推出,
    按其它键继续的话文件为null,所以有异常,只要在第一层中的while中在加入一个读入语句就行了
    try 

       inputStream=new   ObjectInputStream(new   FileInputStream("student.dat")); 

    catch(IOException   e) 

       System.out.println("读取文件出现错误"); 
       System.exit(0);