我正在开发一个大型 GUI 程序,其中我有很多项目数据需要locally
按命令存储。
目前,我将所有全局数据结构保存在保存类(项目)中,然后将它们序列化到本地硬盘文件:
public void saveChanges() {
ArrayList<Student> studentList = new ArrayList<>();
ArrayList<String> coursesList = new ArrayList<>();
ArrayList<Requirement> reqList = new ArrayList<>();
ArrayList<Risk> riskList = new ArrayList<>();
for (int x = 0; x < dlm.getSize(); x++) {
studentList.add(dlm.elementAt(x));
}
for (int x = 0; x < dlm2.getSize(); x++) {
coursesList.add(dlm2.elementAt(x));
}
for (int x = 0; x < dlm3.getSize(); x++) {
reqList.add(dlm3.elementAt(x));
}
for (int x = 0; x < riskTable.getRowCount(); x++) {
riskList.add((Risk) riskMap.get(dtm1.getValueAt(x, 0)));
}
project.setStudentAL(studentList);
project.setCoursesAL(coursesList);
project.setReqAL(reqList);
project.setRiskAL(riskList);
project.setLastUpdated(new Date().toString());
}
现在我将其序列化为本地文件:
public void saveProject(boolean defaultPath, String path) {
saveChanges();
FileOutputStream outFile;
try {
if (defaultPath) {
outFile = new FileOutputStream(directory + project.getProjectName() + ".proj");
} else {
outFile = new FileOutputStream(path + ".proj");
}
ObjectOutputStream outObject = new ObjectOutputStream(outFile);
outObject.writeObject(project);
outObject.close();
} catch (IOException e) {
System.out.println("Failed to save project");
e.printStackTrace();
}
}
我的问题是:有默认值吗?或更好的方法将文件保存在本地硬盘上?我不想使用 XML 或任何数据库。
- 提前致谢