Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Save Object to file , how to using Serialization and deserialize

Thread view: 
moonhk - 22 Oct 2006 16:52 GMT
import java.util.Date;
public class OWRS {
    public String S1;
    public Date currDate  ;

}

import java.io.*;
import java.util.Date;
import java.text.*;
public class OWS {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("theTime");
        ObjectOutputStream s = new ObjectOutputStream(out);
        System.out.println("Write Object to file theTime");
        OWRS me = new OWRS();
        me.S1 = "Today";
        me.currDate = new Date();
        //s.writeObject("Today");
        //s.writeObject(new Date());
        s.writeObject(me);
        s.flush();
}

}

Runtime error
D:\Example\javaux\IO>java OWS
Write Object to file theTime
Exception in thread "main" java.io.NotSerializableException: OWRS
       at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1081)
       at
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)
       at OWS.main(OWS.java:25)

D:\Example\javaux\IO>
dixiecko - 22 Oct 2006 17:05 GMT
Your OWRS Class should implement java.util.Serializable interface

> import java.util.Date;
> public class OWRS {
[quoted text clipped - 29 lines]
>
> D:\Example\javaux\IO>
Lew - 22 Oct 2006 19:48 GMT
>> import java.util.Date;
>> public class OWRS {
[quoted text clipped - 11 lines]
>> java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)
>>         at OWS.main(OWS.java:25)

> Your OWRS Class should implement java.util.Serializable interface

There are also methods to define, UIDs to assign - merely implementing
Serializable is not sufficient.

One should also reconsider making instance variables public.  Furthermore, the
convention for non-constant variables is to name them with an initial
lower-case letter.

>   public String S1;

to
  private String s1;

with appropriate accessor methods.

- Lew
moonhk - 23 Oct 2006 16:48 GMT
Thank Tested, it Works.

import java.util.Date;
public class OWRS implements java.io.Serializable  {
    public String s1;
    public Date currDate  ;

}

> Your OWRS Class should implement java.util.Serializable interface
>
[quoted text clipped - 31 lines]
> >
> > D:\Example\javaux\IO>
Lew - 24 Oct 2006 12:52 GMT
(post re-ordered to eliminate top posting)

>>> untime error
>>> D:\Example\javaux\IO>java OWS
>>> Write Object to file theTime
>>> Exception in thread "main" java.io.NotSerializableException: OWRS

>> Your OWRS Class should implement java.util.Serializable interface

> Thank Tested, it Works.
>
[quoted text clipped - 4 lines]
>
> }

Until you change the implementation of OWRS.

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.