Serializing Objects and reading data in

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy

    #1

    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.generateMainH ash();

    fname = new File("Data");
    try {
    ObjectOutputStr eam oos = new ObjectOutputStr eam(new FileOutputStrea m(fname));
    oos.writeObject (f);
    oos.close();
    System.out.prin tln("Spam Database successfully created");


    // read the database
    //ObjectInputStre am ois = new ObjectInputStre am(new FileInputStream (fname));
    //fcopy = (Filter)ois.rea dObject();
    //ois.close();
    //System.out.prin tln("Data read in successfully");
    }
    catch (IOException e) {
    System.out.prin tln("Fatal Error (0): Writing Data");
    System.exit(1);
    }
    //catch (ClassNotFoundE xception cnfe) {
    // System.out.prin tln("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
    ObjectInputStre am ois = new ObjectInputStre am(new FileInputStream (fname));
    fcopy = (Filter)ois.rea dObject();
    ois.close();
    System.out.prin tln("Data read in successfully");
    }
    catch (IOException e) {
    System.out.prin tln("Fatal Error (0): Reading Data");
    System.exit(1);
    }
    catch (ClassNotFoundE xception cnfe) {
    System.out.prin tln("Fatal Error (1): Class not found. Reading Data");
    System.exit(1);
    }

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

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

Working...