2

I have this to send string or integer but if i want to send a string array what should i use?

  // A string ("Hello, World").
    out.write("Hello, World".getBytes());

    // An integer (123).
    out.write(ByteBuffer.allocate(4).putInt(123).array());

Thanks in advance

1
  • 1
    I highly recommend using .getBytes( "UTF-8" ) to get safe byte encoding for your string. Commented Aug 19, 2011 at 20:53

1 Answer 1

6

Just write the array

ObjectOutputStream out = ...
String[] array = ...     
out.writeObject(array);

If you're using ObjectOutputStream, there's no need to muck about with byte arrays - the class provides high-level methods to read and write entire objects.

Similarly:

out.writeInt(123);
out.writeObject("Hello, World");

You only need to use the write(byte[]) methods if you're using the raw, low-level OutputStream class.

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

3 Comments

Not to mention safe string encoding.
how can one read the same written array using ObjectInputStream readObject()?
@Kush: By casting it. Arrays are objects.

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.