Save Object to file , how to using Serialization and deserialize

Discussion in 'Java' started by moonhk, Oct 22, 2006.

  1. moonhk

    moonhk Guest

    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>
    moonhk, Oct 22, 2006
    #1
    1. Advertising

  2. moonhk

    dixiecko Guest

    Your OWRS Class should implement java.util.Serializable interface

    On Oct 22, 5:52 pm, "moonhk" <> wrote:
    > 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, Oct 22, 2006
    #2
    1. Advertising

  3. moonhk

    Lew Guest

    > On Oct 22, 5:52 pm, "moonhk" <> wrote:
    >> import java.util.Date;
    >> public class OWRS {
    >> public String S1;
    >> public Date currDate ;
    >>
    >> }
    >> 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)


    dixiecko wrote:
    > 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
    Lew, Oct 22, 2006
    #3
  4. moonhk

    moonhk Guest

    Thank Tested, it Works.

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

    }


    dixiecko wrote:
    > Your OWRS Class should implement java.util.Serializable interface
    >
    > On Oct 22, 5:52 pm, "moonhk" <> wrote:
    > > 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>
    moonhk, Oct 23, 2006
    #4
  5. moonhk

    Lew Guest

    (post re-ordered to eliminate top posting)

    >> On Oct 22, 5:52 pm, "moonhk" <> wrote:
    >>> untime error
    >>> D:\Example\javaux\IO>java OWS
    >>> Write Object to file theTime
    >>> Exception in thread "main" java.io.NotSerializableException: OWRS


    > dixiecko wrote:
    >> Your OWRS Class should implement java.util.Serializable interface
    >>


    moonhk wrote:
    > Thank Tested, it Works.
    >
    > import java.util.Date;
    > public class OWRS implements java.io.Serializable {
    > public String s1;
    > public Date currDate ;
    >
    > }


    Until you change the implementation of OWRS.

    - Lew
    Lew, Oct 24, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pico77
    Replies:
    1
    Views:
    3,264
  2. Edmond wong
    Replies:
    2
    Views:
    5,959
    Edmond wong
    Apr 29, 2005
  3. Replies:
    2
    Views:
    2,124
  4. Replies:
    3
    Views:
    1,008
  5. Dimitri Ognibene
    Replies:
    4
    Views:
    763
    Dimitri Ognibene
    Sep 2, 2006
Loading...

Share This Page