📄 studentfromcfg.java
字号:
package exam.dao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import exam.model.Student;
public class StudentFromCfg implements StudentDao {
private Student[] data;
private int count;
private File studentCfg = new File("student.cfg");
public StudentFromCfg() {
data = new Student[1];
count = 0;
init(studentCfg);
}
public void init(File studentCfg) {
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fis = new FileInputStream(studentCfg);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String str = null;
while ((str = br.readLine()) != null) {
String[] s = str.split(":");
if (data.length == count)
extendData();
data[count++] = new Student(Integer.parseInt(s[0]), s[1], s[2]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void extendData() {
Student[] temp = new Student[data.length + 1];
System.arraycopy(data, 0, temp, 0, data.length);
data = temp;
System.gc();
}
public Student getStudent(int id, String password) {
for (int i = 0; i < count; i++) {
if (data[i].getId() == id && data[i].getPassword().equals(password)) {
return data[i];
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -