📄 flightschedulardaofromfile.java
字号:
package com.tarena.abs.dao;
import com.tarena.abs.model.*;
import java.io.*;
import java.util.*;
public class FlightSchedularDAOFromFile implements FlightSchedularDAO{
private File file;
private InputStream in;
private OutputStream out;
public FlightSchedularDAOFromFile(File file)throws FileNotFoundException{
if(!file.exists()||file.isDirectory()){
throw new FileNotFoundException("文件不存在,或者不是有效文件!");
}
this.file=file;
//in=new FileInputStream(file);
}
public boolean addFlightSchedular(FlightSchedular fs) {
HashSet hs=new HashSet();
InputStream in=null;
ObjectInputStream ois=null;
OutputStream out=null;
ObjectOutputStream oos=null;
try {
in=new FileInputStream(file);
ois=new ObjectInputStream(in);
while(in.available()>0){
hs.add(ois.readObject());
}
hs.add(fs);
} 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);
for(Object obj:hs){
oos.writeObject(obj);
}
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 false;
}
public FlightSchedular getFlightSchedular(String flightNumber) {
InputStream in=null;
ObjectInputStream ois=null;
try {
in=new FileInputStream(file);
ois=new ObjectInputStream(in);
while(in.available()>0){
FlightSchedular fs=(FlightSchedular)ois.readObject();
if(fs.getFlightNumber().equals(flightNumber)){
return fs;
}
}
} 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 removeFlightSchedular(String flightNumber) {
// TODO Auto-generated method stub
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -