1

I have several objects which are assigned attributes based on user input. I then store those objects in a vector, and then write that vector to a file but on deserializing the stored vector, only the first object is read. Here's the code that i have so far:

    public Vector<Cases> registerCase() {
    Vector<Cases> casesVector = new Vector<Cases>(10, 2);
    Scanner myCase = new Scanner(System.in);
    Scanner userChoice = new Scanner(System.in);
    System.out.println("HOW MANY CASES DO YOU WANT TO REGISTER?");
    int count = userChoice.nextInt();
    for (int i = 0; i < count; i++) {
        Cases newCase = new Cases();

        System.out.println("Enter the case name: ");
        newCase.caseName = myCase.nextLine();

        System.out.println("Enter the client's name: ");
        newCase.client = myCase.nextLine();

        System.out.println("Enter the case type: ");
        newCase.caseType = myCase.nextLine();

        if((newCase.caseType.equals("Major")) || newCase.caseType.equals("major")){
            newCase.closedCaseRevenue = majorCasePrice;
        }else {
            newCase.closedCaseRevenue = (int) (0.75 * majorCasePrice);
        }

        casesVector.add(newCase);
    }

    try{
        // Open a file to write to, named SavedCases.sav.
        FileOutputStream saveFile = new FileOutputStream("SavedCases.sav", true);
        ObjectOutputStream save = new ObjectOutputStream(saveFile);
        save.writeObject(casesVector);
        save.close(); 
    }
    catch(Exception exc){
        exc.printStackTrace(); 
    }

    Vector<Cases> comVector = new Vector<Cases>();
    try{
        FileInputStream saveFile = new FileInputStream("SavedCases.sav");
        ObjectInputStream save = new ObjectInputStream(saveFile);
        comVector = (Vector<Cases>) save.readObject();
        System.out.println("The size of the vector is: " + comVector.size());
        save.close(); // This also closes saveFile.
    }
    catch(Exception exc){
        exc.printStackTrace(); 
    }

    for (Cases law_case : comVector) {
        System.out.println("Name: " + law_case.caseName);
        System.out.println("Client: " + law_case.client);
        System.out.println("Case Type: " + law_case.caseType);
        System.out.println("Case State: " + law_case.caseState);
        System.out.println("Case Revenue: " + law_case.closedCaseRevenue);
        System.out.println();

    }

    return casesVector;

}
Trust Birungi
  • 122
  • 1
  • 2
  • 12
  • Write out the length of the vector before you output it to make sure it's what you expect it is. Also write out count. What number of cases are you using? – Chris Gerken Oct 15 '12 at 19:11

2 Answers2

1

Your code is running fine for me and is printing all the registered cases except that I am not appending to the already created file(that's what you want, right?). Couple of checks-

  1. Is the class Cases implementing java.io.Serializable?
  2. What is the visibility of the fields? default/private?
  3. I see that you are not closing FileOutputStream and FileInputStream, is there a reason you are doing so?
k_ssup
  • 338
  • 1
  • 8
  • class Cases is implementing java.io.serializable, the visibility of the fields is not explicitly set, and i've closed bothe FileOutputStream and FileInputStream but the initial problem still persists. I'm starting to think the problem is with appending to the already created file. Any insight on that would be very helpful. – Trust Birungi Oct 15 '12 at 20:44
  • yes, that is the problem, you want to create a new file every time, so just remove append=true from contructor of FileOutputStream. – k_ssup Oct 15 '12 at 21:03
  • Are you saying that there is no way to have the data written to one file and read back the way i intended? – Trust Birungi Oct 15 '12 at 21:46
  • See this - http://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream – k_ssup Oct 15 '12 at 21:56
1

EDIT: So to append to a vector if it already exists...

Check for an existing file using

File f = new File(FileName);
if(f.exists()) 
  /* Read in the vector from the file using an object input stream on the file */
else 
  /* make a new vector */

Then read in your input and output it exactly as you have it there, however when you make the FileOutputStream do not include the true flag, this will cause you to add a new vector each time instead of just overwriting the current vector with the new, correct one.

ORIGINAL

The problem is with your implementation is that you are appending a new array each time you write to the save file. So whenever you try to read from the file, you are always getting that first array you ever made.

I am not sure whether you'd like to just overwrite the array with a new one each time, but you should either read in the current array before you add more cases or not set the append flag to true for the FileWriter constructor depending on your desired result.

Chad Campbell
  • 321
  • 1
  • 9
  • I would like to append the new object data to the Vector in the saved file. – Trust Birungi Oct 15 '12 at 20:36
  • So if you just want to do that, then check for existence of the file first. If it exists then read in the vector that is in it. Then you can manipulate it however you want. If the file doesn't exist then make a new one, then when you are done modifying the vector write it to the file again. – Chad Campbell Oct 16 '12 at 12:51
  • Also - When you write it back to the file, you should not be appending to it, you should be overwriting the file. – Chad Campbell Oct 16 '12 at 14:17
  • How can i implement your suggestion? – Trust Birungi Oct 16 '12 at 17:29
  • Thanks a lot @Chad Campbell, i followed your suggestion and it worked out the way i intended. – Trust Birungi Oct 18 '12 at 10:19