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

📄 branchdaofromfile.java

📁 航空售票系统的代码
💻 JAVA
字号:
package com.tarena.abs.dao;

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

import com.tarena.abs.model.Branch;

public class BranchDaoFromFile implements BranchDAO {
	private File file;
	
	public BranchDaoFromFile(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 Branch getBranch(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();
		} 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()){
			Branch a=(Branch)it.next();
			if(a.getName().equals(name) && a.getPasswd().equals(passwd)){
				return a;
			}
		}
		return null;
	}

	public boolean addBranch(Branch user) {
		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(user);
		try {
			fos=new FileOutputStream(file);
			oos=new ObjectOutputStream(fos);
			oos.writeObject(hs);
			oos.flush();
			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 removeBranch(String name) {
		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()){
			Branch a=(Branch)it.next();
			if(a.getName().equals(name) ){
				b = hs.remove(a);
			}
		}
		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 modifyPassword(String name, String oldPassword,
			String newPassword) {
		FileInputStream fis=null;
		ObjectInputStream ois=null;
		FileOutputStream fos=null;
		ObjectOutputStream oos=null;
		HashSet hs=null;
		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()){
			Branch a=(Branch)it.next();
			if(a.getName().equals(name) && a.getPasswd().equals(oldPassword)){
				a.setPasswd(newPassword);
			}
		}
		try {
			fos=new FileOutputStream(file);
			oos=new ObjectOutputStream(fos);
			oos.writeObject(hs);
			return true;
		} 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 Set getAllBranch() {
		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;
	}

}

⌨️ 快捷键说明

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