Save Object to file , how to using Serialization and deserialize

M

moonhk

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>
 
L

Lew

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
 
M

moonhk

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

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>
 
L

Lew

(post re-ordered to eliminate top posting)
> 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top