报什么错误呀?你的这个程序那天好像谁问过,我还回答了,代码在下面,你Copy过去就可以运行的,没有错误。
import java.io.*;
import java.util.*;public class TestScore {
public TestScore() {
LinkedList list=initialize();
writeScore("C:\\score.ser",list);
LinkedList list2= restoreScore("C:\\score.ser");
displayScore(list2);
} public LinkedList initialize(){
LinkedList link=new LinkedList();
StudentScore s1=new StudentScore("Chinese","abu",90);
link.add(s1);
s1=new StudentScore("Math","abu",91);
link.add(s1);
s1=new StudentScore("English","abu",92);
link.add(s1);
s1=new StudentScore("Chemical","abu",93);
link.add(s1); return link;
} public void writeScore(String fileName, LinkedList list)
{
try
{
FileOutputStream fout = new FileOutputStream(fileName);
ObjectOutputStream oout=new ObjectOutputStream(fout);
oout.writeObject(list);
fout.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public LinkedList restoreScore(String fileName)
{
LinkedList link=null;
try
{
FileInputStream fin=new FileInputStream(fileName);
ObjectInputStream oin=new ObjectInputStream(fin);
Object obj=oin.readObject();
link=(LinkedList)obj;
}
catch(Exception e)
{
e.printStackTrace();
}
return link;
} public void displayScore(LinkedList list)
{
Iterator iter=list.iterator();
while(iter.hasNext())
{
StudentScore ss=(StudentScore)iter.next();
System.out.println(ss.getStudentID()+"'s "+ ss.getCourseName()+" Score is:"+ss.getScore());
} }
public static void main(String[] args) {
TestScore testScore1 = new TestScore();
}}class StudentScore implements Serializable
{
private String courseName, studentID;
private int score; public StudentScore(String courseName, String studentID,int score)
{
this.courseName=courseName;
this.studentID=studentID;
this.score=score;
} public String getCourseName()
{
return this.courseName;
}
public String getStudentID()
{
return this.studentID;
}
public int getScore()
{
return this.score;
}
}
import java.io.*;
import java.util.*;public class TestScore {
public TestScore() {
LinkedList list=initialize();
writeScore("C:\\score.ser",list);
LinkedList list2= restoreScore("C:\\score.ser");
displayScore(list2);
} public LinkedList initialize(){
LinkedList link=new LinkedList();
StudentScore s1=new StudentScore("Chinese","abu",90);
link.add(s1);
s1=new StudentScore("Math","abu",91);
link.add(s1);
s1=new StudentScore("English","abu",92);
link.add(s1);
s1=new StudentScore("Chemical","abu",93);
link.add(s1); return link;
} public void writeScore(String fileName, LinkedList list)
{
try
{
FileOutputStream fout = new FileOutputStream(fileName);
ObjectOutputStream oout=new ObjectOutputStream(fout);
oout.writeObject(list);
fout.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public LinkedList restoreScore(String fileName)
{
LinkedList link=null;
try
{
FileInputStream fin=new FileInputStream(fileName);
ObjectInputStream oin=new ObjectInputStream(fin);
Object obj=oin.readObject();
link=(LinkedList)obj;
}
catch(Exception e)
{
e.printStackTrace();
}
return link;
} public void displayScore(LinkedList list)
{
Iterator iter=list.iterator();
while(iter.hasNext())
{
StudentScore ss=(StudentScore)iter.next();
System.out.println(ss.getStudentID()+"'s "+ ss.getCourseName()+" Score is:"+ss.getScore());
} }
public static void main(String[] args) {
TestScore testScore1 = new TestScore();
}}class StudentScore implements Serializable
{
private String courseName, studentID;
private int score; public StudentScore(String courseName, String studentID,int score)
{
this.courseName=courseName;
this.studentID=studentID;
this.score=score;
} public String getCourseName()
{
return this.courseName;
}
public String getStudentID()
{
return this.studentID;
}
public int getScore()
{
return this.score;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货