📄 userfiledao.java
字号:
package dao.impl;
import java.io.*;
import java.util.*;
import dao.UserDao;
import model.User;
public class UserFileDao implements UserDao {
private String name;
public UserFileDao(String name) {
super();
this.name = name;
}
public void saveUsers(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()){
User ac=(User)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 loadUsers(){
Map m=new HashMap();
ObjectInputStream in=null;
try {
FileInputStream fis=new FileInputStream(name);
in = new ObjectInputStream(fis);
while(true){
User ac=(User)in.readObject();
m.put(new Integer(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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -