📄 student.java
字号:
/*
* Student.java
*
* Created on 2007年12月12日, 下午6:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package 考试成绩分析程序;
import java.io.*;
/**
*
* @author drawen
*/
public class Student implements java.io.Serializable{
public static final String STUDENT_FILE="studentFile.dat";
private long id;
private String name;
/** Creates a new instance of Student */
public Student(long id,String name) {
setId(id);
setName(name);
}
public Student(){}
public void setId(long id){
this.id=id;
}
public long getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public boolean equals(Object o){
Student temp=(Student)(o);
if(id==temp.id)
return true;
else
return false;
}
public boolean saveToFile(){
MainWindow.initDataFile(STUDENT_FILE);
DataOutputStream output;
DataInputStream input;
String name;
long id;
try{
input=new DataInputStream(new FileInputStream(STUDENT_FILE));
while(input.available()>0){
id=input.readLong();
name=input.readUTF();
if(id==this.id){
input.close();
return false;
}
}
input.close();
output=new DataOutputStream(new FileOutputStream(STUDENT_FILE,true));
output.writeLong(this.id);
output.writeUTF(this.name);
output.close();
return true;
}catch(Exception e){
return false;
}
}
public static Student queryFromFile(long studentId){
MainWindow.initDataFile(STUDENT_FILE);
DataInputStream input;
String name;
long id;
try{
input=new DataInputStream(new FileInputStream(STUDENT_FILE));
while(input.available()>0){
id=input.readLong();
name=input.readUTF();
if(id==studentId){
input.close();
return new Student(id,name);
}
}
input.close();
return null;
}catch(Exception e){
return null;
}
}
public static Student[] loadFile(){
Student[] student=new Student[20];
int count=0;
MainWindow.initDataFile(STUDENT_FILE);
DataInputStream input;
try{
input=new DataInputStream(new FileInputStream(STUDENT_FILE));
while(input.available()>0){
if(count>=student.length){
Student[] temp=new Student[student.length*2];
for(int i=0;i<student.length;i++)
temp[i]=student[i];
student=temp;
}
student[count]=new Student();
student[count].setId(input.readLong());
student[count].setName(input.readUTF());
count++;
}
input.close();
return student;
}catch(Exception e){
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -