thieumao

DocGhi.java - Anh Kool

Jul 30th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package Lamlai;
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.util.ArrayList;
  12.  
  13. /**
  14.  *
  15.  * @author leo
  16.  */
  17. public class DocGhi {
  18.  
  19.     public DocGhi() {
  20.     }
  21.  
  22.     public void DocFile(String f, ArrayList<Student> arrSV) {
  23.         try {
  24.             Student test = new Student();
  25.             FileInputStream fis = new FileInputStream(f);
  26.             ObjectInputStream in1 = new ObjectInputStream(fis);
  27.             do {
  28.                 test = (Student) in1.readObject();
  29.                 arrSV.add(new Student(test.getTen(), test.getTuoi()));
  30.             } while (test != null);
  31.             in1.close();
  32.         } catch (Exception e) {
  33. //            System.out.println("Error - Open");
  34.         }
  35.     }
  36.  
  37.     public void GhiFile(String f, ArrayList<Student> arrSV) {
  38.         try {
  39.             FileOutputStream fos = new FileOutputStream(f);
  40.             ObjectOutputStream out = new ObjectOutputStream(fos);
  41.             for (int i = 0; i < arrSV.size(); i++) {
  42.                 Student sv = (Student) arrSV.get(i);
  43.                 out.writeObject(sv);
  44.             }
  45.             out.flush();
  46.             out.close();
  47.         } catch (Exception ee) {
  48.             System.out.println("Error");
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment