accountfiledao.java
来自「这个小的系统是使用java语言编写的一个银行类的小程序」· Java 代码 · 共 67 行
JAVA
67 行
package dao.impl;
import java.io.*;
import java.util.*;
import dao.AccountDao;
import model.Account;
public class AccountFileDao implements AccountDao {
private String name;
public AccountFileDao(String name) {
super();
this.name = name;
}
public void saveAccounts(Map m){
ObjectOutputStream out=null;
try {
FileOutputStream fos=new FileOutputStream(name);
out = new ObjectOutputStream(fos);
Collection c=m.values();
Iterator it=c.iterator();
while(it.hasNext()){
Account ac=(Account)it.next();
out.writeObject(ac);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if (out!=null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Map loadAccounts(){
Map m=new HashMap();
ObjectInputStream in=null;
try {
FileInputStream fis=new FileInputStream(name);
in = new ObjectInputStream(fis);
while(true){
Account ac=(Account)in.readObject();
m.put(new Long(ac.getId()),ac);
}
} catch (IOException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally{
try {
if (in!=null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return m;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?