Link to home
Start Free TrialLog in
Avatar of JavaJedi012400
JavaJedi012400

asked on

Appending to file

I know this question has been asked before but I'm still having problems.

I wish to append data to a file.
The code is as follows:

public void HashSave(Hashtable hash, String fileName){
            try
            {      
                FileOutputStream fos = new FileOutputStream(fileName, true);
                  ObjectOutputStream oos = new ObjectOutputStream(fos);
                  oos.writeObject(hash);
                  oos.flush();
                      fos.close();
            }catch (IOException e){};
      }

The problems is this: If the file doesn't already exist it will create the file and enter the data, which can then be retrieved. However, if it already exists, it will not even append.

NB- since initially writing this I have decided to look in the file to see if data is being appended. It is and so I think there must be something wrong with my read code.

My read code is as follows:
public void HashRead(Hashtable hash, String fileName, String Abb){
            try{
                  FileInputStream fis = new FileInputStream(fileName);
                  ObjectInputStream ois= new ObjectInputStream(fis);
                  hash = (Hashtable)ois.readObject();
                  System.out.println(hash.get(Abb));
            }catch (IOException e){}catch (ClassNotFoundException c){};
      }.

So, where's the problem? As you can see, I have done the append. Also, I am using JDK 1.2, though I am not sure if that makes a difference.

Thanks.
Avatar of heyhey_
heyhey_

ObjectStreams use special headers. so if you open two output streams and write two objects, you have to open TWO ObjectInputStreams to read the objects.
Avatar of JavaJedi012400

ASKER

OK, thanks. However, I actually want to save all entries into one hashtable, and then when I call the hashtable I wish to read the string that is mapped to a specified key. I think I may be putting all entries into a different hashtable, though saving it in the one file. I am only calling the first hashtable.

I have to stress that I am new to Java, no more than a weeks experience. I hope I haven't confused you and hope you can help me.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the best solution is to NOT APPEND to a file. ObjectOutputStream will write its own headers at the very beggining, which will be in the middle of your physical file, so second ObjectInputStream won't be able to read.

note that you can write multiple objects to a single ObjectOutputStream, but you can't (you'd better not) append ObjectOutputStream to ObjectOutputStream.

otherwise your code is ok :)
Much appreciated for your answer. It was a simple issue I see now, but it did confuse me as I have only been Java programming for a week. The issue has been cleared for me. Again, thankyou.