📄 accountfiledao.java
字号:
package dao.impl;
import java.util.*;
import java.io.*;
import model.*;
import dao.AccountDao;
public class AccountFileDao implements AccountDao {
private String fileName;
public AccountFileDao(String fileName){
this.fileName=fileName;
}
public Map loadAccounts() {
Map accounts=new HashMap();
ObjectInputStream in=null;
try {
FileInputStream fis=new FileInputStream(fileName);
in=new ObjectInputStream(fis);
while(true){
Object o=in.readObject();
Account c=(Account)o;
accounts.put(new Long(c.getId()),c);
}
}catch (IOException e) {
} catch (ClassNotFoundException e) {
}
finally{
try {
if (in!=null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return accounts;
}
public void saveAccounts(Map accounts) {
ObjectOutputStream out=null;
try {
FileOutputStream fos=new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
Collection as=accounts.values();
Iterator it=as.iterator();
while(it.hasNext()){
Object o=it.next();
out.writeObject(o);
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if (out!=null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -