package dao; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import pojo.Book; public class BookDao { public BookDao() { super(); } public void object2File(ArrayList<Book> bookList){ ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("d:/books.dat")); oos.writeObject(bookList); oos.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { oos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public ArrayList<Book> file2Object(){ ArrayList<Book> bookList = new ArrayList<Book>(); File f = new File("d:/books.dat"); if(f.exists()){ ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("d:/books.dat")); bookList = (ArrayList)ois.readObject(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return bookList; } }