📄 accountfiledao.java
字号:
package DAO;import java.util.*;import java.io.*;import model.*;/* * 文件DAO的实现,将系统中的所有有用数据信息保存到文件中; * saveAccounts(Map map):将传过来的Map中的数据保存到指定(name文件名)的文件中; * loadAccounts():以自己的id为key值 */class AccountFileDAO implements AccountDAO { private String name; public AccountFileDAO(String name) { super(); this.name = name; } public void saveAccounts(HashMap map) { ObjectOutputStream out = null; try { FileOutputStream fos = new FileOutputStream(name); out = new ObjectOutputStream(fos); Collection c = map.values(); Iterator it = c.iterator(); if (it.hasNext()) { Account a = (Account) it.next(); out.writeObject(a); } } catch (FileNotFoundException e) { // e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } } public HashMap loadAccounts() { HashMap map = null; ObjectInputStream in = null; try { map = new HashMap(); FileInputStream fin = new FileInputStream(name); in = new ObjectInputStream(fin); while (true) { Account ac = (Account) in.readObject(); map.put(new Long(ac.getId()), ac); } } catch (FileNotFoundException e) { // e.printStackTrace(); } catch (IOException e) { } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } return map; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -