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.
JavaJedi012400Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
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.
0
JavaJedi012400Author Commented:
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.
0
heyhey_Commented:
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 :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
heyhey_Commented:
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 :)
0
JavaJedi012400Author Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.