Advertisement
thieumao

QuanLyFile

Sep 11th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package xuly;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.util.ArrayList;
  11.  
  12. import base.CanBo;
  13.  
  14. public class QuanLyFile {
  15.    
  16.     private static final String FILE_NAME = "asm4.dat";
  17.  
  18.     public static boolean hasObject(File file) {
  19.         FileInputStream fileInputStream;
  20.         boolean check = true;
  21.         try {
  22.             fileInputStream = new FileInputStream(file);
  23.             ObjectInputStream inStream = new ObjectInputStream(fileInputStream);
  24.             if (inStream.readObject() == null) {
  25.                 check = false;
  26.             }
  27.             inStream.close();
  28.         } catch (FileNotFoundException e) {
  29.             check = false;
  30.         } catch (IOException e) {
  31.             check = false;
  32.         } catch (ClassNotFoundException e) {
  33.             check = false;
  34.         }
  35.         return check;
  36.     }
  37.  
  38.     public static boolean write(CanBo canBo) {
  39.         try {
  40.             File file = new File(FILE_NAME);
  41.             FileOutputStream fileOutputStream;
  42.             ObjectOutputStream outStream = null;
  43.             if (!file.exists()) {
  44.                 fileOutputStream = new FileOutputStream(file);
  45.                 outStream = new ObjectOutputStream(fileOutputStream);
  46.             } else {
  47.                 if (!hasObject(file)) {
  48.                     fileOutputStream = new FileOutputStream(file);
  49.                     outStream = new ObjectOutputStream(fileOutputStream);
  50.                 } else {
  51.                     fileOutputStream = new FileOutputStream(file, true);
  52.                     outStream = new ObjectOutputStream(fileOutputStream) {
  53.                         protected void writeStreamHeader() throws IOException {
  54.                             reset();
  55.                         }
  56.                     };
  57.                 }
  58.             }
  59.             outStream.writeObject(canBo);
  60.             outStream.close();
  61.         } catch (IOException e) {
  62.             return false;
  63.         }
  64.         return true;
  65.     }
  66.  
  67.     public static ArrayList<CanBo> read() {
  68.         ArrayList<CanBo> dsCanBo = new ArrayList<CanBo>();
  69.         try {
  70.             File file = new File(FILE_NAME);
  71.             FileInputStream fileInputStream = new FileInputStream(file);
  72.             ObjectInputStream inStream = new ObjectInputStream(fileInputStream);
  73.             Object object;
  74.             while (true) {
  75.                 object = inStream.readObject();
  76.                 dsCanBo.add((CanBo) object);
  77.             }
  78.         } catch (ClassNotFoundException e) {
  79.         } catch (IOException e) {
  80.         }
  81.         return dsCanBo;
  82.     }
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement