StreamCorruptedException when searching for an object in a file

2

Good, I'm doing an exercise in which I must add some objects to a file using a method and another method to find them using the name given to them.

I put the methods and classes:

Article Class, this is the Object to insert.

public class Articulo implements Serializable{

private String nombre;
private int cantidad;
private double precioUnidad;

public Articulo(String nombre, int cantidad, double precioUnidad){
    this.nombre=nombre;
    this.cantidad=cantidad;
    this.precioUnidad=precioUnidad;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public int getCantidad() {
    return cantidad;
}

public void setCantidad(int cantidad) {
    this.cantidad = cantidad;
}

public double getPrecioUnidad() {
    return precioUnidad;
}

public void setPrecioUnidad(double precioUnidad) {
    this.precioUnidad = precioUnidad;
}

public void vende(int num) throws NoDisponible{
    if(this.cantidad>0){
        this.cantidad=this.cantidad-num;
    }else{
        throw new NoDisponible("No hay artículos disponibles");
    }
}

public String toString(){
    return "El articulo se llama "+nombre+" y hay "+cantidad;
}
}

This is the Store class, with the insert and search methods:

public class Tienda {

String directorio="C://Users//prh19//Desktop//tienda.txt";
ObjectOutputStream ficheroObjeto=null;
ObjectInputStream ficheroObjetoLectura=null;

public void insertar(Articulo a){
    try {
        ficheroObjeto=new ObjectOutputStream(new FileOutputStream(directorio,true));
        ficheroObjeto.writeObject(a);
        System.out.println("Objeto insertado correctamente");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(ficheroObjeto!=null){
            try{
                ficheroObjeto.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

public Articulo busca(String nombreArticulo) throws NoExiste{
    Articulo articulo=null;
    try {
        ficheroObjetoLectura=new ObjectInputStream(new FileInputStream(directorio));
        articulo=(Articulo) ficheroObjetoLectura.readObject();
        while(articulo!=null){
            if(articulo instanceof Articulo){

                articulo=(Articulo) ficheroObjetoLectura.readObject();
                if(articulo.getNombre().equals(nombreArticulo)){
                    return articulo;
                }else{
                    throw new NoExiste("Ese articulo no existe");
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }finally{
        if(ficheroObjetoLectura!=null){
            try {
                ficheroObjetoLectura.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return articulo;

}
}

This is my main class:

public class PruebaTienda {

public static void main(String[] args) throws NoExiste {

    Tienda tienda=new Tienda();
    Articulo a1=new Articulo("Patatas",0,10.5);
    Articulo a2=new Articulo("Tomates",5,1.5);
    Articulo a3=new Articulo("Ajos",456,40.5);
    Articulo a4=new Articulo("Olivas",45,10.90);
    Articulo a5=new Articulo("Carne",23,9.87);
    Articulo a6=new Articulo("Lechuga",89,11.56);

    tienda.insertar(a1);
    tienda.insertar(a2);
    tienda.insertar(a3);
    tienda.insertar(a4);
    tienda.insertar(a5);
    tienda.insertar(a6);
    System.out.println(tienda.busca("Patatas"));
    System.out.println(tienda.busca("Tomates"));
}

}

At the time of inserting an Article I have no problem, the problem comes when it comes to looking for it, that I get this:

java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
 El articulo se llama Patatas y hay 0
at java.io.ObjectInputStream.readObject(Unknown Source)
at examenfebrero.Tienda.busca(Tienda.java:39)
at examenfebrero.PruebaTienda.main(PruebaTienda.java:21)  


java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at examenfebrero.Tienda.busca(Tienda.java:39)
at examenfebrero.PruebaTienda.main(PruebaTienda.java:22)
El articulo se llama Patatas y hay 0
    
asked by NeoChiri 15.02.2017 в 14:59
source

2 answers

0

I think you have to reset, try putting this

if(ficheroObjeto!=null){
            try {
                ficheroObjeto.close();
                ficheroObjeto.reset();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
answered by 15.02.2017 в 16:08
0

You have an error in the busca method (in the finally ), you have to close ficheroObjetoLectura and not ficheroObjeto . You should use the notation

try(ObjectInputStream  ficheroObjetoLectura=new ObjectInputStream(new FileInputStream(directorio))){
...
}

This saves you the manual management of close() :

public Articulo busca(String nombreArticulo) throws NoExiste{
    Articulo articulo=null;
    try(FileOutputStream miFOS = new FileOutputStream(directorio)){
        try(ObjectInputStream  ficheroObjetoLectura=new ObjectInputStream(miFOS)){
            articulo=(Articulo) ficheroObjetoLectura.readObject();
            while(articulo!=null){
                if(articulo instanceof Articulo){

                    articulo=(Articulo) ficheroObjetoLectura.readObject();
                    if(articulo.getNombre().equals(nombreArticulo)){
                        return articulo;
                    }else{
                        throw new NoExiste("Ese articulo no existe");
                    }
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
            e.printStackTrace();
    }
    return articulo;

}
    
answered by 15.02.2017 в 15:11