package com.syit.lmis.bankroll.util;import java.io.Serializable;/**
* 学生成绩
* @author biggie
*/
public class StudentScore implements Serializable {
/** 学业名称 */
private String courseName;
/** 学生ID */
private String studentID;
/** 分数 */
private int score;
public StudentScore(String courseName, String studentID,int score){
this.courseName=courseName;
this.studentID=studentID;
this.score=score;
} /**
* Returns the courseName.
* @return String
*/
public String getCourseName() {
return courseName;
} /**
* Returns the score.
* @return int
*/
public int getScore() {
return score;
} /**
* Returns the studentID.
* @return String
*/
public String getStudentID() {
return studentID;
} /**
* Sets the courseName.
* @param courseName The courseName to set
*/
public void setCourseName(String courseName) {
this.courseName = courseName;
} /**
* Sets the score.
* @param score The score to set
*/
public void setScore(int score) {
this.score = score;
} /**
* Sets the studentID.
* @param studentID The studentID to set
*/
public void setStudentID(String studentID) {
this.studentID = studentID;
}}
* 学生成绩
* @author biggie
*/
public class StudentScore implements Serializable {
/** 学业名称 */
private String courseName;
/** 学生ID */
private String studentID;
/** 分数 */
private int score;
public StudentScore(String courseName, String studentID,int score){
this.courseName=courseName;
this.studentID=studentID;
this.score=score;
} /**
* Returns the courseName.
* @return String
*/
public String getCourseName() {
return courseName;
} /**
* Returns the score.
* @return int
*/
public int getScore() {
return score;
} /**
* Returns the studentID.
* @return String
*/
public String getStudentID() {
return studentID;
} /**
* Sets the courseName.
* @param courseName The courseName to set
*/
public void setCourseName(String courseName) {
this.courseName = courseName;
} /**
* Sets the score.
* @param score The score to set
*/
public void setScore(int score) {
this.score = score;
} /**
* Sets the studentID.
* @param studentID The studentID to set
*/
public void setStudentID(String studentID) {
this.studentID = studentID;
}}
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
/**
* <p></p>
* @author biggie
*/
public class Test { public static void main(String[] args) {
Test t= new Test();
LinkedList list=t.initialize();
t.writeScore("score.ser",list);
LinkedList list2= t.restoreScore("score.ser");
t.displayScore(list2);
}
/**
* 在此创建多个StudentScore对象,并添加到LinkedList中
* @return LinkedList @see java.util.LinkedList
*/
public LinkedList initialize(){
LinkedList list = new LinkedList();
for(int i=0;i<10;i++){
StudentScore studentScore=new StudentScore("数学","学号",i);
list.add(studentScore);
}
return list;
}
/**
* 负责向指定的文件序列化指定链表中的对象
* @param fileName 序列化文件名
* @param list LinkedList对像
*/
public void writeScore(String fileName, LinkedList list){
try{
FileOutputStream ostream = new FileOutputStream(fileName);// 构造文件输出流
ObjectOutputStream p = new ObjectOutputStream(ostream);// 绑定
p.writeObject(list); // 输出继承了序列化接口的类
p.flush();
p.close();
ostream.close();
}catch(Exception e){
System.out.println(e);
}
}
/**
* 从指定的文件中反序列化StudentScore对象到链表中
* @param fileName 序列化文件名
* @return LiskedList
*/
public LinkedList restoreScore(String fileName){
LinkedList list =new LinkedList();
try{
FileInputStream istream = new FileInputStream(fileName); // 构造文件输入流
ObjectInputStream pr = new ObjectInputStream(istream); // 绑定
list = (LinkedList)pr.readObject(); // 读入序列化的类
istream.close();
}catch(Exception e){
System.out.println(e);
}
return list;
}
/**
* 从指定的链表中依次打印每个对象的成绩信息
* @param list
*/
public void displayScore(LinkedList list){
for(int i=0;i<list.size();i++){
StudentScore studentScore = (StudentScore)list.get(i);
System.out.println("---------------------------------------");
System.out.println("----课目----");
System.out.println(studentScore.getCourseName());
System.out.println("----学号----");
System.out.println(studentScore.getStudentID());
System.out.println("----分数----");
System.out.println(studentScore.getScore());
System.out.println("---------------------------------------");
}
}
}
佩服,你还真是有时间啊。
建议楼主给biggie(飞碟)80给我20!
哈哈。