Results 1 to 5 of 5

Thread: Serialization

  1. #1
    thayalan is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default Serialization

    Hello there,

    I am utilizing serialization functionality of Java to Save the work done using the application that I have developed. When the user goes to File -> Save, the application calls the following function:

    public void save()
    {

    try{
    System.out.println("before FOS");

    FileOutputStream f_out = new FileOutputStream("save.data");

    System.out.println("after FOS");
    ObjectOutputStream out = new ObjectOutputStream (f_out);

    System.out.println("after OOS");
    out.writeObject(myPanel);
    //myPanel is an instance of JPanel which contains all the sketches done by the user.

    out.close();
    f_out.close();
    System.out.println("after writeObject");
    }
    catch(Exception e)
    {
    System.out.println("Exception in Save function");
    }
    }


    This particular code runs fine but the problem is that the name of the serialized file is hard coded which is, "save.data" as highlighted in blue in the above code.
    So I added the JFileChooser component to enable the user to type in the file name that he/she wants. The following is the amended code:


    public void save()
    {
    JFileChooser chooserSave = new JFileChooser();
    chooserSave.setCurrentDirectory(new File ("."));


    returnVal2 = chooserSave.showSaveDialog(myPanel);
    File fileName = chooserSave.getSelectedFile();
    System.out.println("filename : " + fileName);

    if(returnVal2 == JFileChooser.APPROVE_OPTION)
    {
    try{
    System.out.println("before FOS");

    FileOutputStream f_out = new FileOutputStream(fileName);

    System.out.println("after FOS");
    ObjectOutputStream out = new ObjectOutputStream (f_out);

    System.out.println("after OOS");
    out.writeObject(myPanel);

    out.close();
    f_out.close();
    System.out.println("after writeObject");
    }
    catch(Exception e)
    {
    System.out.println("Exception in Save function");
    }

    }
    }


    With the amended code however, the programs throws exception after blue line which means writeObject method could not be performed.
    Could anyone kindly advice what could be wrong? I have made almost all the possible amendments to the best of my knowledge but to no avail.
    Thank you very much.

  2. #2
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    Hmm basic code looks fine to me, is your jpanel Serialized? Only problem I could think of.

  3. #3
    thayalan is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    Yes JPanel is serialized. Basically myPanel is instantiated from class CustomPanel which implements java.io.Serializable. The weird thing about this problem is that if the file name is hard coded, it works perfectly, in fact I tried to open the saved file and it works flawlessly. So i suspect the problem might be with the JFileChoose doesn't seem to get along with the writeObject method but I am clueless on how to rectify it. Any more hints?

  4. #4
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    Well can you give some more information? Print out the errors etc?

    Java Code:
    public void save()
    {
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File ("."));
    
        int result = fc.showSaveDialog(myPanel);
        if (result == JFileChooser.APPROVE_OPTION) {
            File fileName = fc.getSelectedFile();
            System.out.println("filename : " + fileName);
            FileOutputStream fos = null;
            ObjectOutputStream out = null;
            try {
                fos = new FileOutputStream(fileName);
                out = new ObjectOutputStream(fos);
                out.writeObject(myPanel);
            } catch (Exception e) {
                System.out.println("Error "+e.getMessage());
            } finally {
                try {
                    out.close(); fos.close();
                } catch (Exception e) {}
            }
        }
    }
    hmm try the above code. I can't test it right now but should be fine. If it doesn't work, then show us the errors. It will most likely be a problem finding the file if its in the FOS :/

  5. #5
    vkorenev is offline Member
    Join Date
    Aug 2009
    Location
    Kharkov, Ukraine
    Posts
    29
    Rep Power
    0

    Default

    I'd like to warn you that this approach to save users work is not robust in long run. I'd rather serialise to XML.

    Although XML is not the best format for representing trees as text. Both YAML and JSON are more readable by humans.

Similar Threads

  1. Serialization
    By vijay24805 in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-10-2009, 10:16 PM
  2. Java serialization
    By paul in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 09:58 PM
  3. about serialization
    By bishnu in forum New To Java
    Replies: 0
    Last Post: 12-19-2008, 10:13 AM
  4. Need help using serialization
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 09:23 PM
  5. Replies: 0
    Last Post: 04-04-2008, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •