Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. /**
  2. * Crea una cuenta y la guarda en un fichero.
  3. * @param c
  4. * @throws IOException
  5. * @throws ClassNotFoundException
  6. */
  7. public void crearCuenta(Cuenta c) throws IOException, ClassNotFoundException {
  8.  
  9. boolean existe = comprobarExistencia(c);
  10.  
  11. if(existe==true) System.err.println("Existe el objeto, amigo.");
  12. else {
  13. ObjectOutputStream ostream = new ObjectOutputStream(new FileOutputStream("/media/sda1/unai11794/cuentas.dat",true));
  14.  
  15. ostream.writeObject(c);
  16. ostream.close();
  17. }
  18. }
  19.  
  20. /**
  21. * Comprueba que un objeto existe en el fichero cuentas.dat
  22. * @param c es la cuenta a buscar
  23. * @return
  24. * @throws FileNotFoundException
  25. * @throws IOException
  26. * @throws ClassNotFoundException
  27. */
  28. public boolean comprobarExistencia(Cuenta c) throws FileNotFoundException, IOException, ClassNotFoundException{
  29. //EOF End Of File
  30. boolean EOF = false, existe = false;
  31. ObjectInputStream istream = new ObjectInputStream(new FileInputStream(new File("/media/sda1/unai11794/cuentas.dat")));
  32.  
  33. //lee objetos mientras no se lance la excepcion de end of file.
  34. //en caso que se lance, EOF es true.
  35. while(!EOF){
  36. try{
  37. //se guarda el objeto del fichero en una instancia de Cuenta.
  38. Cuenta temporal = (Cuenta) istream.readObject();
  39.  
  40. if(temporal.equals(c)){
  41. EOF = true;
  42. existe = true;
  43. }
  44. } catch(EOFException e){
  45. EOF = true;
  46. }
  47. }
  48.  
  49. istream.close();
  50. return existe;
  51. }
  52.  
  53. public static void main(String[] args) {
  54. Cuenta a = new Cuenta("00001","Ahorros",113,0,500);
  55. Cuenta b = new Cuenta("00003","Viajes",58,2,3000);
  56. try {
  57. a.crearCuenta(a);
  58. b.crearCuenta(b);
  59. } catch (IOException | ClassNotFoundException e) {
  60. System.err.println(e);
  61. }
  62. }
  63.  
  64. sr cajero.CuentaI
  65. autorizadoFsaldoItitularLn_cuentatLjava/lang/String;Lnombreq~xpE;:t00003tViajes
  66.  
  67. java.io.StreamCorruptedException: invalid type code: AC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement