📄 agentdaofromfile.java
字号:
package com.tarena.abs.dao;
import java.util.*;
import java.io.*;
import com.tarena.abs.model.Agent;
public class AgentDAOFromFile implements AgentDAO {
private File file;
public AgentDAOFromFile(File file){
this.file=file;
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) {
e.printStackTrace();
}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=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} 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;
}
public boolean addAgent(Agent user) {
// TODO Auto-generated method stub
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
if(hs!=null){
Iterator it=hs.iterator();
while(it.hasNext()){
Agent a=(Agent)it.next();
if(user.getName().equals(a.getName()))
return false;
}
}
hs.add(user);
try {
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return true;
}
public boolean removeAgent(String name) {
// TODO Auto-generated method stu
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} 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 {
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
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) {
// TODO Auto-generated method stub
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
if(hs!=null){
Iterator it=hs.iterator();
while(it.hasNext()){
Agent a=(Agent)it.next();
if(a.getName().equals(name)){
a.setPasswd(newPassword);
try {
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
}catch(Exception e){
return false;
}
return true;
}
}
}
return false;
}
public Set getAllAgent() {
// TODO Auto-generated method stub
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
return null;
}
return hs;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -