By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,696 Members | 1,424 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,696 IT Pros & Developers. It's quick & easy.

Serializing Objects and reading data in

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.