📄 planemodeldaofromfile.java
字号:
package com.tarena.abs.dao;
import java.util.*;
import java.io.*;
import com.tarena.abs.model.PlaneModel;
public class PlaneModelDaoFromFile implements PlaneModelDAO {
private File file;
public PlaneModelDaoFromFile(String fileName){
file=new File(fileName);
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();
System.out.println("没有找到"+fileName+"文件");
}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();
System.out.println("创建了"+fileName+"文件");
} 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 Set getAllPlaneModel() {
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
return hs;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
return null;
}
public boolean addPlaneModel(PlaneModel plane) {
FileInputStream fis=null;
ObjectInputStream ois=null;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
HashSet hs=null;
boolean b=false;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)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){}
}
b = hs.add(plane);
try {
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
return b;
} catch (Exception e) {
e.printStackTrace();
} finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
return false;
}
public boolean removePlaneModel(String model) {
FileInputStream fis=null;
ObjectInputStream ois=null;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
HashSet hs=null;
boolean b = false;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)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){}
}
Iterator it=hs.iterator();
while(it.hasNext()){
PlaneModel pm=(PlaneModel)it.next();
if(pm.getModel().equals(model) ){
b = hs.remove(pm);
}
}
try {
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
return b;
} catch (Exception e) {
e.printStackTrace();
} finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -