可以运行 你适当在修改一下吧import java.util.*;
import java.io.*;
public class TestSer
{
public static void main(String[] args)
{
LinkedList l1 = init();
writeScore("ming.bat",l1);
LinkedList l2 = readScore("ming.bat");
displayScore(l2);
}
public static LinkedList init()
{
LinkedList ll = new LinkedList();
StudentScore s1 = new StudentScore("java",123,60);
StudentScore s2 = new StudentScore("linux",124,78);
StudentScore s3 = new StudentScore("xxx",125,89);
ll.add(s1);
ll.add(s2);
ll.add(s3);
return ll;
}
public static void writeScore(String fileName,LinkedList list)
{
try
{
ObjectOutputStream oos =
new ObjectOutputStream(new FileOutputStream(fileName));
int i = list.size();
StudentScore[] ss = new StudentScore[i];
for(int j = 0;j<i;j++)
{
ss[j]=(StudentScore)list.get(j);
}
oos.writeObject(ss);
oos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static LinkedList readScore(String fileName)
{
LinkedList list = new LinkedList();
try
{
ObjectInputStream ois =
new ObjectInputStream(new FileInputStream(fileName));
StudentScore[] ss = (StudentScore[])ois.readObject();
ois.close();
for(int i = 0;i<ss.length;i++)
{
list.add(ss[i]);
}
}
catch(ClassNotFoundException e)
{
}
catch(IOException e)
{
e.printStackTrace();
}
return list;
}
public static void displayScore(LinkedList list)
{
for(int i = 0;i<list.size();i++)
{
System.out.println(((StudentScore)list.get(i)).toString());
}
}
}class StudentScore implements Serializable
{
private String courceName;
private int studentID;
private int score;
public StudentScore()
{}
public StudentScore(String courceName,int studentID,int score)
{
this.courceName = courceName;
this.studentID = studentID;
this.score = score;
}
public String toString()
{
return "courceName: "+courceName+" StudentID: "+studentID+" score: "+score;
}
}
import java.io.*;
public class TestSer
{
public static void main(String[] args)
{
LinkedList l1 = init();
writeScore("ming.bat",l1);
LinkedList l2 = readScore("ming.bat");
displayScore(l2);
}
public static LinkedList init()
{
LinkedList ll = new LinkedList();
StudentScore s1 = new StudentScore("java",123,60);
StudentScore s2 = new StudentScore("linux",124,78);
StudentScore s3 = new StudentScore("xxx",125,89);
ll.add(s1);
ll.add(s2);
ll.add(s3);
return ll;
}
public static void writeScore(String fileName,LinkedList list)
{
try
{
ObjectOutputStream oos =
new ObjectOutputStream(new FileOutputStream(fileName));
int i = list.size();
StudentScore[] ss = new StudentScore[i];
for(int j = 0;j<i;j++)
{
ss[j]=(StudentScore)list.get(j);
}
oos.writeObject(ss);
oos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static LinkedList readScore(String fileName)
{
LinkedList list = new LinkedList();
try
{
ObjectInputStream ois =
new ObjectInputStream(new FileInputStream(fileName));
StudentScore[] ss = (StudentScore[])ois.readObject();
ois.close();
for(int i = 0;i<ss.length;i++)
{
list.add(ss[i]);
}
}
catch(ClassNotFoundException e)
{
}
catch(IOException e)
{
e.printStackTrace();
}
return list;
}
public static void displayScore(LinkedList list)
{
for(int i = 0;i<list.size();i++)
{
System.out.println(((StudentScore)list.get(i)).toString());
}
}
}class StudentScore implements Serializable
{
private String courceName;
private int studentID;
private int score;
public StudentScore()
{}
public StudentScore(String courceName,int studentID,int score)
{
this.courceName = courceName;
this.studentID = studentID;
this.score = score;
}
public String toString()
{
return "courceName: "+courceName+" StudentID: "+studentID+" score: "+score;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货