Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. public static void guardarListado(ArrayList<Persona> listado, String ruta) {
  2. File fichero = new File(ruta);
  3. try {
  4.  
  5. FileOutputStream ficEscritu = new FileOutputStream(fichero);
  6.  
  7. ObjectOutputStream guardarObjetos = new ObjectOutputStream(ficEscritu);
  8.  
  9. for (Persona per : listado) {
  10. guardarObjetos.writeObject(per);
  11. }
  12. guardarObjetos.close();
  13. } catch (IOException e) {
  14. }
  15. }
  16.  
  17. public static ArrayList volcarFicheroEnUnArrayList(String ruta) {
  18. Persona persona;
  19. ArrayList listado = new ArrayList();
  20. FileInputStream fichLectura = null;
  21. File fichero = new File(ruta);
  22. if (fichero.isFile() && fichero.exists()) {
  23. try {
  24. fichLectura = new FileInputStream(fichero);
  25. ObjectInputStream leerObjetos = new ObjectInputStream(fichLectura);
  26. while (true) {
  27. persona = (Persona) leerObjetos.readObject();
  28. listado.add(persona);
  29. }
  30.  
  31. } catch (EOFException e) {
  32. System.out.println("Hemos volcado la informacion de los empleados en el array");
  33. try {
  34. fichLectura.close();
  35. } catch (IOException ex) {
  36. Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, null, ex);
  37. }
  38. } catch (FileNotFoundException ex) {
  39. System.out.println("No existe el fichero");
  40. } catch (IOException ex) {
  41. Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, null, ex);
  42. } catch (ClassNotFoundException ex) {
  43. Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, null, ex);
  44. }
  45.  
  46. } else {
  47. System.out.println("No existe el fichero");
  48. }
  49. //
  50.  
  51. return listado;
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement