471,108 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

Serializing Objects and reading data in

Hi,

In the code below (not pretty I know but it's an early version :-P) I'm
having problems reading the data object back in. If I move the reading
code to immediately after the section where it is written ( commented
out in code) then it reads in OK. However, when I move the code to the
right place ( as shown here) it throws an IO exception. Both the Filter
class and it's constituent class implement Serializable and it would seem
the data is there as it can read it into a new Filter object (fcopy).

Any suggestions please? I'm fairly new to Java.

Cheers

Andy
public static void main(String[] args) {

int i = args.length;

Filter f = new Filter();
Filter fcopy = new Filter();
File fname;

if (i > 0 ) {

if (args[0].equals("-learn") && i == 3) {
f.learn(args[1], args[2]); // lookup the spam and ham data
f.generateMainHash();

fname = new File("Data");
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fname));
oos.writeObject(f);
oos.close();
System.out.println("Spam Database successfully created");
// read the database
//ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fname));
//fcopy = (Filter)ois.readObject();
//ois.close();
//System.out.println("Data read in successfully");
}
catch (IOException e) {
System.out.println("Fatal Error (0): Writing Data");
System.exit(1);
}
//catch (ClassNotFoundException cnfe) {
// System.out.println("Fatal Error (1): Class not found. Reading Data");
// System.exit(1);
//}
} else {
if (args[0].equals("-scan") && i == 2) {
fname = new File("Data");
try {
// read the spam database
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fname));
fcopy = (Filter)ois.readObject();
ois.close();
System.out.println("Data read in successfully");
}
catch (IOException e) {
System.out.println("Fatal Error (0): Reading Data");
System.exit(1);
}
catch (ClassNotFoundException cnfe) {
System.out.println("Fatal Error (1): Class not found. Reading Data");
System.exit(1);
}

fcopy.spamTest(args[1]);
} else {
System.out.println("Usage: MainFilter -learn <spam folder> <ham filter>\n" +
" MainFilter -scan <testfolder>");
}

}
} else {
System.out.println("Usage: MainFilter -learn <spam folder> <ham filter>\n" +
" MainFilter -scan <testfolder>");
}
}
}

Jul 18 '05 #1
0 3375

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ivo Bronsveld | last post: by
2 posts views Thread by Tobias Zimmergren | last post: by
4 posts views Thread by Dave Veeneman | last post: by
12 posts views Thread by Cagdas Ozgenc | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.