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

1
  • 1
    Can you give more details about your problem? A sample code would be better. Commented Apr 10, 2011 at 11:53

2 Answers 2

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..

}
Sign up to request clarification or add additional context in comments.

Comments

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;
       }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.