Results 1 to 5 of 5
Thread: Serialization
- 07-28-2009, 07:44 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
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.
- 07-28-2009, 09:25 PM #2
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
Hmm basic code looks fine to me, is your jpanel Serialized? Only problem I could think of.
- 07-29-2009, 06:28 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
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?
- 07-29-2009, 11:39 AM #4
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
Well can you give some more information? Print out the errors etc?
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 :/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) {} } } }
- 08-03-2009, 10:22 PM #5
Member
- Join Date
- Aug 2009
- Location
- Kharkov, Ukraine
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
Serialization
By vijay24805 in forum Threads and SynchronizationReplies: 1Last Post: 04-10-2009, 09:16 PM -
Java serialization
By paul in forum Advanced JavaReplies: 3Last Post: 04-10-2009, 08:58 PM -
about serialization
By bishnu in forum New To JavaReplies: 0Last Post: 12-19-2008, 09:13 AM -
Need help using serialization
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 08:23 PM -
What is Serialization and de-serialization in Java
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks