2
\$\begingroup\$

I usually use the static methods from this designed class in order to perform objects serializing of custom classes.

Even though I'm getting good results, I wonder if this is the most appropriate way for it and if there are any improvements for it?

public class InputOuput {        
    private static FileOutputStream         flujoSalida;
    private static ObjectOutputStream       flujoSalidaObjetos;
    private static File                     archivo;
    private static final FileChooser        seleccionador = new FileChooser();

    private static FileInputStream          flujoEntrada;
    private static ObjectInputStream        flujoEntradaObjetos;

    public static boolean serialize(Object anObject) {            
        seleccionador.setTitle("Dónde guardar el estado del programa");

        try {                
            archivo = seleccionador.showSaveDialog(null);
            if (archivo == null) return false;

            flujoSalida = new FileOutputStream(archivo);
            flujoSalidaObjetos = new ObjectOutputStream(flujoSalida);
            flujoSalidaObjetos.writeObject(anObject);
            flujoSalidaObjetos.close();
            flujoSalida.close();
        } catch (IOException error) {
            System.out.println(error.toString());
            System.out.println(error.getStackTrace());
        }            
        return true;            
    }

    public static Object deserialize() {                
        Object m=null;

        try {                
            archivo = seleccionador.showOpenDialog(null);
            if (archivo == null) return null;
            flujoEntrada = new FileInputStream(archivo);
            flujoEntradaObjetos = new ObjectInputStream(flujoEntrada);
            m= (Object) flujoEntradaObjetos.readObject();
            flujoEntradaObjetos.close();
            flujoEntrada.close();
        } catch (IOException | ClassNotFoundException error) {
            System.out.println(error.toString());
            System.out.println(error.getStackTrace());
        }            
        return m;
    }        
}

*I change Object for the custom intended class

One good point to ask: how could I be not making the InputOutput class to implement Serializable, yet it works properly anyway?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ When you have variable names like "m", you should always think of renaming. \$\endgroup\$ – Kinjal Nov 4 '13 at 5:08
  • \$\begingroup\$ I agree @Kinjal :) \$\endgroup\$ – diegoaguilar Mar 13 '14 at 16:27
2
\$\begingroup\$

All of your variables should be local, except for maybe the FileChooser.

You swallow exceptions. Printing a stack trace is no substitution for proper exception handling. (By the way, logging should be done using java.util.logging. If you must print a stack trace manually, print it to System.err instead of System.out.) Serialization could completely fail, and the function will still return true. The most appropriate behaviour should be to propagate the exception to the caller.

For greater flexibility and clarity with little additional effort, I suggest overloading serialize() and deserialize() with several variants which call each other:

public class InputOutput {
    private static final FileChooser seleccionador = new FileChooser();

    /**
     * Serializes an object to a file chosen by the user using a FileChooser.
     * @return true if completed, false if user cancels the FileChooser.
     */
    public static boolean serialize(Object o) throws IOException {
        seleccionador.setTitle("Dónde guardar el estado del programa");
        File f = seleccionador.showSaveDialog(null);
        if (f == null) return false;
        serialize(o, f);
        return true;
    }

    public static void serialize(Object o, File f) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(f)) {
            serialize(o, fos);
        }
    }

    public static void serialize(Object o, FileOutputStream fos) throws IOException {
        try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(o);
        }
    }

    /**
     * Deserializes an object from a file chosen by the user using a FileChooser.
     * @return the deserialized object, or null if user cancels the FileChooser.
     */
    public static Object deserialize() throws IOException { ... }
    public static Object deserialize(File f) throws IOException { ... }
    public static Object deserialize(FileInputStream fis) throws IOException { ... }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.