3

I have an ObjectInputStream which needs to read two different inputs which are a String and my own created object. I have a thread which constantly waits for an input and depending on the input be it a string or object it will process the result. I need a way for the input to be able to distinguish between the two.

Any help would be great.

Thanks

bubblebath
  • 939
  • 4
  • 18
  • 45

2 Answers2

3

Isn't it a case of doing:

if (objectFromStream instanceof YourObejct) {

    YourObject obj = (YourObject) objectFromStream;
    ....

} else if (objectFromStream instanceof String) {

    String str = (String) objectFromStream;

} else {
  // throw excepption..

}
Richard H
  • 38,037
  • 37
  • 111
  • 138
2

If the only two choices are Sting and your object then you can try this:

    Object obj = ois.readObject();
    if(obj instanceof String){
       String s = (String)obj;
    }else{
       if(obj instanceof MyObject){
          MyObject m = (MyObject)obj;
       }

    }
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192