Can a boolean be modified in a serializable binary file?

0

As I read on some websites, you can not modify the object, but you can change the value of a Boolean variable. The point is that I do it in the following way and it does not show any error, but when I make the modification and print the object it does not change anything. Is it possible to modify it?

public void ExistMoto (String file, String NameMoto) {

    try {   
        MotObjectOutputStream objectOS = new MotObjectOutputStream(new FileOutputStream(fichero,true)); 
        ObjectInputStream objectIS = new ObjectInputStream(new FileInputStream(fichero));

        Object ObjMotos = objectIS.readObject();

        while (ObjMotos != null) {

            if (ObjMotos instanceof Moto) {
                String ObjNombre = (((Moto) ObjMotos).getNombre());
                if (ObNombrej.equals(NombreMoto)) { //Comprobar nombre existe

                    ((Moto) ObjMotos).setExiste(false);
                    objectOS.writeObject(ObjMotos);         
                    objectIS.close();   
                }else {
                }        
                ObjMotos = objectIS.readObject();
            }
        }
        System.out.println("Se ha cambiado el estado"); 
        objectIS.close();       
    }
}

Any help is welcome. Thanks in advance!

    
asked by Mautista 25.01.2018 в 22:30
source

2 answers

0

I have not tested your code, but I would say that the problem is that you are trying to serialize / deserialize objects in the same file at the same time.

Try dividing the function in these steps:

  • Open file, deserialize objects, close file.
  • Modify state of objects ( setExiste(false) ).
  • Open file, serialize objects, close file.
  • answered by 26.01.2018 / 07:51
    source
    0

    Good, we are going to go by parties so as not to get involved.

    Regarding the class that you created MotObjectOutputStream that extends from ObjectOutputStream , if you have not changed anything, directly use the parent class. It is only a recommendation, it would only serve you if in the future you plan to modify something, which I doubt. But that's not the problem here.

    The first thing to understand is how the writing and reading channels of files work. In this case you are using new FileOutputStream(fichero,true) initialize one. If you go to the documentation on that constructor you will see that what you are saying is that when you pass it a true as the second parameter you are telling it that whatever you type through that channel will be added to the end of the file. That means that whatever you're trying to write will be written after the objects that already exist.

    With which, if when you want to check if the first object you find has been modified, it will always say no, because what you have done is write a new object and add it to the end.

    Binary files with objects have a disadvantage, if you want to modify one, the normal thing is that you create a new file and write everything you find and when you find the object you want to modify, you do not write that but the modified one .

    In addition, writing an object, closing the channel and reopening it to rewrite another object can cause failures when reading the file. In this question already I explained something similar.

    I hope it has been helpful. If you have any questions, I'll be here.

        
    answered by 26.01.2018 в 21:43