Advertisement
Kabbalah

Exemplo DAO - Java

Dec 2nd, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. package br.com.exemplo.model;
  2.  
  3. import br.com.exemplo.beans.Funcionario;
  4.  
  5. import java.io.*;
  6. import java.util.ArrayList;
  7.  
  8. @SuppressWarnings("ALL")
  9. public class EmployeeDao {
  10.     private File arquivo;
  11.  
  12.     public EmployeeDao() {
  13.         this.arquivo = new File("funcionarios.data");
  14.     }
  15.  
  16.     public void escreverArquivo(ArrayList<Funcionario> funcionarios) throws IOException {
  17.         FileOutputStream fos = new FileOutputStream(arquivo);
  18.         ObjectOutputStream oos = new ObjectOutputStream(fos);
  19.  
  20.         oos.writeObject(funcionarios);
  21.  
  22.         oos.flush();
  23.         oos.close();
  24.         fos.close();
  25.     }
  26.  
  27.     public ArrayList<Funcionario> lerArquivo() throws IOException, ClassNotFoundException {
  28.         ArrayList<Funcionario> funcionarios = null;
  29.  
  30.         if (!arquivo.exists()) {
  31.             arquivo.createNewFile();
  32.             funcionarios = new ArrayList<>();
  33.             funcionarios.add(new Funcionario("Marcelo", "Regis", "Masculino", "81 99999-9999", "999.999.999-99", "admin", "123", true));
  34.             escreverArquivo(funcionarios);
  35.         } else {
  36.             FileInputStream fis = new FileInputStream(arquivo);
  37.             ObjectInputStream ois = new ObjectInputStream(fis);
  38.  
  39.             funcionarios = (ArrayList<Funcionario>) ois.readObject();
  40.  
  41.             ois.close();
  42.             fis.close();
  43.         }
  44.  
  45.         return funcionarios;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement