Advertisement
anhit92

GiFile

Jun 15th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package L05;
  6.  
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.ObjectInputStream;
  12. import java.io.ObjectOutputStream;
  13. import java.util.ArrayList;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author Anh-Kool
  20.  */
  21. public class GiFile {
  22.  
  23.     public String file = "data121.txt";
  24.  
  25.     void write(ArrayList<Student> students) {
  26.         try {
  27.             ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file, true));
  28.             oos.writeObject(students);
  29.             oos.close();
  30.         } catch (IOException ex) {
  31.             System.out.println("Ghi file loi");
  32.         }
  33.     }
  34.  
  35.     ArrayList<Student> read() {
  36.         ArrayList<Student> students = new ArrayList<Student>();
  37.         File file1 = new File(file);
  38.         try {
  39.             if (file1 != null) {
  40.                 FileInputStream fis = new FileInputStream(file);
  41.                 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
  42.                 students = (ArrayList<Student>) ois.readObject();
  43.             }
  44.         } catch (IOException ex) {
  45.             Logger.getLogger(GiFile.class.getName()).log(Level.SEVERE, null, ex);
  46.         } catch (ClassNotFoundException ex) {
  47.             Logger.getLogger(GiFile.class.getName()).log(Level.SEVERE, null, ex);
  48.         }
  49.         return students;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement