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

📄 fileoperation.java

📁 旅馆管理系统主要功能有添加
💻 JAVA
字号:
/**
 * 
 */
package dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;

import biz.Holiday_co;
import biz.Hotel;


/**
 * 文件操作,读取或存入信息到指定文件
 * @author 邹志军
 *
 */
public class FileOperation {
	
	/**
	 * 从文件中读取Hotel数据,并存放到 LinkedList<Hotel>
	 * @param filePath
	 * @return 含有数据的链表
	 */
	public static LinkedList<Hotel> readHotelFile(String filePath){
		LinkedList<Hotel> hotels=new LinkedList<Hotel>();
		FileInputStream filein=null;
		ObjectInputStream objectin=null;
		try {
			filein=new FileInputStream(filePath);
			objectin=new ObjectInputStream(filein);
			Hotel hotel=(Hotel)objectin.readObject();
			//读信息
			while(hotel!=null){
				hotel.print();
				hotels.add(hotel);
				hotel=(Hotel)objectin.readObject();
			}
		} catch (IOException e) {			

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				if(objectin!=null)
					objectin.close();
				if(filein!=null)
					filein.close();
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}		
		return hotels;
	}
	/**
	 * 从Holiday文件中读取Holiday_co信息,并存放到LinkedList<Holiday_co>
	 * @param filePath
	 * @return 带有Holiday_co信息的链表
	 */
	public static LinkedList<Holiday_co> readHolidayFile(String filePath){
		LinkedList<Holiday_co> holis=new LinkedList<Holiday_co>();
		FileInputStream filein=null;
		ObjectInputStream objectin=null;
		try {
			filein=new FileInputStream(filePath);
			objectin=new ObjectInputStream(filein);
			Holiday_co holiday=(Holiday_co)objectin.readObject();
			//读信息
			while(holiday!=null){
				holiday.print();
				holis.add(holiday);
				holiday=(Holiday_co)objectin.readObject();
			}
		} catch (IOException e) {			

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				if(objectin!=null)
					objectin.close();
				if(filein!=null)
					filein.close();
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}
		return holis;
	}
	/**
	 * 将链表hotels中的信息存入到filePath路径下的文件中去
	 * @param filePath
	 * @param hotels
	 */
	public static void writeHotelFile(String filePath,LinkedList<Hotel> hotels){
		//链表长度要大于0hotels.size()>0 && 
		if( filePath!=null ){

			File hotelfile=new File(filePath);
			if(hotelfile.isDirectory()){
				hotelfile=new File(filePath+"hotel.txt");
			}
			if(hotelfile.exists()){
				hotelfile.getParentFile().mkdirs();
				try {
					hotelfile.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			//存储信息
			FileOutputStream fileout=null;
			ObjectOutputStream objectout=null;
			try {
				fileout = new FileOutputStream(hotelfile);
				objectout=new ObjectOutputStream(fileout);
				int i=0;
				for(;i<hotels.size();i++){
					Hotel hotel=hotels.get(i);
					hotel.print();
					objectout.writeObject(hotel);
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					objectout.close();
					fileout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 将链表holis中的信息存入到filePath路径下的文件中去
	 * @param filePath
	 * @param holis
	 */
	public static void writeHolidayFile(String filePath,LinkedList<Holiday_co> holis){
		//链表长度要大于0
		if(holis.size()>0 && filePath!=null){

			File hotelfile=new File(filePath);
			if(hotelfile.isDirectory()){
				hotelfile=new File(filePath+"hotel.txt");
			}
			if(hotelfile.exists()){
				hotelfile.getParentFile().mkdirs();
				try {
					hotelfile.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			//存储信息
			FileOutputStream fileout=null;
			ObjectOutputStream objectout=null;
			try {
				fileout = new FileOutputStream(hotelfile);
				objectout=new ObjectOutputStream(fileout);
				int i=0;
				for(;i<holis.size();i++){
					Holiday_co hotel=holis.get(i);
					hotel.print();
					objectout.writeObject(hotel);
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					objectout.close();
					fileout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		LinkedList<Hotel> hotels=new LinkedList<Hotel>();
		LinkedList<Holiday_co> holis=new LinkedList<Holiday_co>();
		
		hotels.add(new Hotel(1,"武夷大酒店","福州市",300,120,200,250,300,150));
		hotels.add(new Hotel(2,"黄金大酒店","三明市",100,100,30,50,199,20));
		String filePath="D:/workspace/hotelCtrlSystem/src/edu/hotel.txt";
		FileOperation.writeHotelFile(filePath, hotels);
		
		holis.add(new Holiday_co(1,"欢乐度假村","闽侯县",50,20,1000));
		holis.add(new Holiday_co(2,"农家度假村","昌乐县",100,30,1500));
		String filePath2="D:/workspace/hotelCtrlSystem/src/edu/holiday.txt";
		FileOperation.writeHolidayFile(filePath2, holis);
		
		
		
	}
}

⌨️ 快捷键说明

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