先这样吧... 目前已实现基本逻辑: - 添加一个学生 - 按名字查找学生 - 按学号查找学生 - 对学生列表进行排序 - 获取全班某课程的成绩信息 - 获取全班所有同学的总成绩信息 - 从文件中读取学生数据 希望没什么bugmain
parent
bea60a3774
commit
3e3c93bb48
@ -0,0 +1,43 @@ |
||||
package me.lensfrex.manager.utils; |
||||
|
||||
import me.lensfrex.manager.data.Student; |
||||
|
||||
import java.io.EOFException; |
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.ObjectInputStream; |
||||
import java.util.ArrayList; |
||||
|
||||
/** |
||||
* 参考自以前的项目: |
||||
* https://github.com/lensferno/dogename/blob/main/Dogename/src/main/java/me/lensferno/dogename/data/History.java
|
||||
*/ |
||||
public class DataLoader { |
||||
private static final String DATA_FILE = "files/students.data"; |
||||
|
||||
/**\ |
||||
* 从默认路径(files/student.data)加载学生数据,若文件不存在或读取过程中发生错误,则新建一个空的ArrayList |
||||
* @return 加载读取的结果,若文件不存在或读取过程中发生错误,则返回一个空的ArrayList |
||||
*/ |
||||
public ArrayList<Student> load() { |
||||
File historyFile = new File(DATA_FILE); |
||||
try { |
||||
if (!historyFile.exists()) { |
||||
historyFile.getParentFile().mkdirs(); |
||||
historyFile.createNewFile(); |
||||
return new ArrayList<>(); |
||||
} |
||||
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(historyFile)); |
||||
ois.close(); |
||||
return (ArrayList<Student>) ois.readObject(); |
||||
} catch (EOFException e) { |
||||
System.out.println("Data file is empty."); |
||||
return new ArrayList<>(); |
||||
} catch (Exception e) { |
||||
System.out.println("Failed to load data file:" + e); |
||||
e.printStackTrace(); |
||||
return new ArrayList<>(); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue