Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Crea una cuenta y la guarda en un fichero.
- * @param c
- * @throws IOException
- * @throws ClassNotFoundException
- */
- public void crearCuenta(Cuenta c) throws IOException, ClassNotFoundException {
- boolean existe = comprobarExistencia(c);
- if(existe==true) System.err.println("Existe el objeto, amigo.");
- else {
- ObjectOutputStream ostream = new ObjectOutputStream(new FileOutputStream("/media/sda1/unai11794/cuentas.dat",true));
- ostream.writeObject(c);
- ostream.close();
- }
- }
- /**
- * Comprueba que un objeto existe en el fichero cuentas.dat
- * @param c es la cuenta a buscar
- * @return
- * @throws FileNotFoundException
- * @throws IOException
- * @throws ClassNotFoundException
- */
- public boolean comprobarExistencia(Cuenta c) throws FileNotFoundException, IOException, ClassNotFoundException{
- //EOF End Of File
- boolean EOF = false, existe = false;
- ObjectInputStream istream = new ObjectInputStream(new FileInputStream(new File("/media/sda1/unai11794/cuentas.dat")));
- //lee objetos mientras no se lance la excepcion de end of file.
- //en caso que se lance, EOF es true.
- while(!EOF){
- try{
- //se guarda el objeto del fichero en una instancia de Cuenta.
- Cuenta temporal = (Cuenta) istream.readObject();
- if(temporal.equals(c)){
- EOF = true;
- existe = true;
- }
- } catch(EOFException e){
- EOF = true;
- }
- }
- istream.close();
- return existe;
- }
- public static void main(String[] args) {
- Cuenta a = new Cuenta("00001","Ahorros",113,0,500);
- Cuenta b = new Cuenta("00003","Viajes",58,2,3000);
- try {
- a.crearCuenta(a);
- b.crearCuenta(b);
- } catch (IOException | ClassNotFoundException e) {
- System.err.println(e);
- }
- }
- sr cajero.CuentaI
- autorizadoFsaldoItitularLn_cuentatLjava/lang/String;Lnombreq~xpE;:t00003tViajes
- java.io.StreamCorruptedException: invalid type code: AC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement