⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 planemodeldaofromfile.java

📁 一套基于JAVA开发的完整版航空订票系统,代码简洁,适合JAVA初学者研究
💻 JAVA
字号:
package com.tarena.abs.dao;

import java.io.*;
import java.util.*;

import com.tarena.abs.model.PlaneModel;

public class PlaneModelDaoFromFile implements PlaneModelDAO {	
	private File file;
	
	public PlaneModelDaoFromFile(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) {
			System.out.println("planeModel.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 Set getAllPlaneModel() {
		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();
		}
		return hs;
	}

	@SuppressWarnings("unchecked")
	public boolean addPlaneModel(PlaneModel plane) {
		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(plane);//集合中添加一个对象
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(hs);//把集合对象从新写入文件中
			oos.flush();
			oos.close();
			
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public boolean removePlaneModel(String model) {
		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()){
			PlaneModel a = (PlaneModel)it.next();
			if(a.getModel().equals(model)){
				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;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -