0

I need to store objects in files (.txt, .mio, .Whatever)

I'm using java

I have tried to implement the Serialize class to serialize objects and then store them, but I could not get the desired result.

EDIT

The idea is to store multiple objects in the same file and then open the file and look for a specific object, manipulate objects. etc. it is as if it were a SQL database only with objects stored in files.

which would be a solution to this? a way of storing objects in files and then you can call them, search for them, store them in an array.

Would appreciate any idea and help.

CODE

   import java.io.Serializable;

public class Contacto implements Serializable {

    private String nombre;
    private String apellido;

    public Contacto(String nombre, String apellido){
        this.nombre = nombre;
        this.apellido = apellido;
    }

    public String getNombre(){
        return this.nombre;
    }

    public String getApellido(){
        return this.apellido;
    }

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

    public void setApellido(String a){
        this.apellido = a;
    }

    public String toString(){
        return this.getApellido() +" "+ this.getNombre();
    }
}

////////////////////////////////////////////////////////////////////////////

import java.io.*;

public class Serializador{

    // Escribe un objecto en un archivo
    private ObjectOutputStream escritorArchivo;

    // Lee un objecto que este guardado en un archivo
    private ObjectInputStream lectorArchivo;


    // Al metodo le pasamos el objeto que queremos serializar y lo guardará en el archivo que se le especifique al FileOutputStream (en este caso "objeto.mio")
    public void escribirArchivo(Object objeto){
        try{
            escritorArchivo = new ObjectOutputStream(new FileOutputStream("objeto.mio"));
            escritorArchivo.writeObject(objeto);
        } catch(FileNotFoundException fnfex){
            fnfex.printStackTrace();
        } catch(IOException ioex){
            ioex.printStackTrace();
        }
    }

    public Object leerArchivo(String rutaArchivo){
        Object lectura = null;
        try{
            lectorArchivo = new ObjectInputStream(new FileInputStream(rutaArchivo));
            lectura = lectorArchivo.readObject();
        } catch(FileNotFoundException fnfex){
            fnfex.printStackTrace();
        } catch(IOException ioex){
            ioex.printStackTrace();
        } catch(ClassNotFoundException cnfex){
            cnfex.printStackTrace();
        }
        return lectura;
    }

}

the code shows how the basic idea, however it is an idea that can be discarded.

Thank you very much.

Reco Jhonatan
  • 1,503
  • 4
  • 23
  • 35
  • can you paste your code .. to get more clarity on what you are looking for? – mhasan Sep 21 '16 at 05:51
  • @mhasan see de edit – Reco Jhonatan Sep 21 '16 at 05:54
  • you code seems to perform the logic of reading and writing the object state to file. Are you looking for converting and storing it as json format in the file? – mhasan Sep 21 '16 at 06:04
  • no, sorry if I did misunderstand. what I want is to store multiple objects in a file and then be able to open that file and make search, call an object in specific and suchlike. – Reco Jhonatan Sep 21 '16 at 06:06
  • Hi @Reco, this post should help..http://stackoverflow.com/questions/3470138/writing-many-java-objects-to-a-single-file – mhasan Sep 21 '16 at 06:11

0 Answers0