📄 agentdaofromfile.java
字号:
package com.tarena.abs.dao;
import java.io.*;
import java.util.*;
import com.tarena.abs.model.Agent;
public class AgentDaoFromFile implements AgentDAO {
private File file;
public AgentDaoFromFile(File flie){
this.file = flie;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
FileInputStream fis=null;
ObjectInputStream ois=null;
Object obj=null;
try {//试着读取文件中的对象
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
obj=ois.readObject();
} catch (Exception e) {
System.out.println("agent.dat文件中目前还没有数据,将写入一个空数据!");
}finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
//如果读到的对象为null,或者不是HashSet
if(obj==null || !(obj instanceof HashSet)){
try {//就向文件中写入一个空的HashSet
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(new HashSet());
oos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
}
}
public Agent getAgent(String name, String passwd) {
FileInputStream fis = null;
ObjectInputStream ois = null;
HashSet hs = new HashSet();
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
hs = (HashSet)ois.readObject();
ois.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
Iterator it = hs.iterator();
while(it.hasNext()){
Agent a = (Agent)it.next();
if(a.getName().equals(name)&& a.getPasswd().equals(passwd)){
return a;
}
}
return null;
}
@SuppressWarnings("unchecked")
public boolean addAgent(Agent user) {
FileInputStream fis = null;
ObjectInputStream ois = null;
HashSet hs = new HashSet();
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
hs = (HashSet)ois.readObject();//从文件中读取对象
ois.close();//关闭文件输入流
fis.close();
hs.add(user);//集合中添加一个对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(hs);//把集合对象从新写入文件中
oos.flush();
oos.close();
} catch (Exception e) {
return false;
}
return true;
}
public boolean removeAgent(String name) {
FileInputStream fis = null;
ObjectInputStream ois = null;
HashSet hs = new HashSet();
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
hs = (HashSet)ois.readObject();
ois.close();
fis.close();
} catch (Exception e) {
return false;
}
Iterator it = hs.iterator();
while(it.hasNext()){
Agent a = (Agent)it.next();
if(a.getName().equals(name)){
hs.remove(a);
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(hs);
oos.flush();
oos.close();
}catch (IOException e) {
return false;
}
return true;
}
}
return false;
}
public Agent updateScore(String name, int n) {
FileInputStream fis = null;
ObjectInputStream ois = null;
HashSet hs = new HashSet();
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
hs = (HashSet)ois.readObject();
ois.close();
fis.close();
} catch (Exception e) {
return null;
}
Iterator it = hs.iterator();
while(it.hasNext()){
Agent a = (Agent)it.next();
if(a.getName().equals(name)){
a.setScore(a.getScore()+n);
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(hs);
oos.flush();
oos.close();
}catch (IOException e) {
return null;
}
return a;
}
}
return null;
}
public boolean modifyPassword(String name, String oldPassword,
String newPassword) {
FileInputStream fis = null;
ObjectInputStream ois = null;
HashSet hs = new HashSet();
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
hs = (HashSet)ois.readObject();
ois.close();
fis.close();
} catch (Exception e) {
return false;
}
Iterator it = hs.iterator();
while(it.hasNext()){
Agent a = (Agent)it.next();
if(a.getName().equals(name) && a.getPasswd().equals(oldPassword)){
a.setPasswd(newPassword);
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(hs);
oos.flush();
oos.close();
}catch (IOException e) {
return false;
}
return true;
}
}
return false;
}
public Set getAllAgent() {
FileInputStream fis = null;
ObjectInputStream ois = null;
HashSet hs = new HashSet();
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
hs = (HashSet)ois.readObject();
ois.close();
fis.close();
} catch (Exception e) {
return null;
}
return hs;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -