472,809 Members | 3,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 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 3506

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
4
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm...
0
by: JackRazz | last post by:
I'm trying to serialize a collection to a file stream by serializing each object individually. The code below works fine with the BinaryFormatter, but the SoapFormatter reads the first object and...
0
by: Camilo | last post by:
I'm trying to make a progress bar for when our client application begins to upload files to a webservice. I have done a soapextension in the client-side that tries to read the position of the...
1
by: Martin Widmer | last post by:
Hi guys I am wondering what is a proper way to persistently store objects into SQL-Server. I see four possible ways: 1.) Serialize to XML and then store the XML in the SQL server 2.) Write a...
47
by: Max | last post by:
Due to the behaviour of a particular COM object, I need to ensure that a request for a particular ASP page is finalized before another request for the page is processed. Does IIS have a way to...
12
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.