• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Serialization

 
vi ch
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code I need to serialize a hastable and make it persistant. In the following code,before reading an object I need to check if the file is empty. Does anyone know how to do that?
I tried checking the ObjectInputStream for null, but that doesnt work. Can anyone help?
FileOutputStrem out=new FileOutputStream(s);
ObjectOutputStream O=new ObjectOutputStream(out);
FileInputStream in=new FileInputStream(s);
ObjectInputStream I=new ObjectInputStream(in);

if(I!=null)
calList=(Hashtable)I.readObject();
Thanks
Vidyu Challa
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean to check if the file is 0 bytes?
why would there be such a file?
you can try to see if the file exists using the exists() method in the File class.
if theer is such a file then read it and see if the hashtable you get is equal to null.
thats ahould work , unless i completly misunderstood your question and then im sorry.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy's right, you're checking to see if your ObjectInputStream is null, not if you are reading anything from the file.
What you want is something like this:

That will check if there was already a HashTable to read from the file. If none was returned with readObject(), you create a new one.
There is a File.length() method that will tell you the length of the file in bytes, but I don't believe that is what you are searching for.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!