📄 planemodeldaofromfile.java
字号:
package com.tarena.abs.dao;
import java.util.*;
import java.io.*;
import com.tarena.abs.model.*;
public class PlaneModelDAOFromFile implements PlaneModelDAO {
/*
public Set getAllPlaneModel() {
// TODO Auto-generated method stub
return null;
}
public boolean addPlaneModel(PlaneModel plane) {
// TODO Auto-generated method stub
return false;
}
public boolean removePlaneModel(String model) {
// TODO Auto-generated method stub
return false;
}
*/
private File file;
public PlaneModelDAOFromFile(File file) {
this.file=file;
OutputStream out=null;
ObjectOutputStream oos=null;
try {
if(!(file.length()>0)){
out=new FileOutputStream(file);
oos=new ObjectOutputStream(out);
oos.writeObject(new HashSet());
oos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(out!=null)try{out.close();}catch(IOException e){}
}
}
public boolean addPlaneModel(PlaneModel plane) {
HashSet hs=null;
InputStream in=null;
ObjectInputStream ois=null;
OutputStream out=null;
ObjectOutputStream oos=null;
boolean success=false;
try {
in=new FileInputStream(file);
ois=new ObjectInputStream(in);
hs=(HashSet)ois.readObject();
if(hs==null){
hs=new HashSet();
}
success=hs.add(plane);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(in!=null)try{in.close();}catch(IOException e){}
}
try {
out=new FileOutputStream(file);
oos=new ObjectOutputStream(out);
oos.writeObject(hs);
oos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(out!=null)try{out.close();}catch(IOException e){}
}
return success;
}
public Set getAllPlaneModel() {
HashSet hs=null;
InputStream in=null;
ObjectInputStream ois=null;
try {
in=new FileInputStream(file);
ois=new ObjectInputStream(in);
hs=(HashSet)ois.readObject();
return hs;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(in!=null)try{in.close();}catch(IOException e){}
}
return null;
}
public boolean removePlaneModel(String model) {
// TODO Auto-generated method stub
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -