This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Counterpart to ObjecOutputStream#writeChars? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Counterpart to ObjecOutputStream#writeChars?" Watch "Counterpart to ObjecOutputStream#writeChars?" New topic

Counterpart to ObjecOutputStream#writeChars?

Peter Wolfenstein

Joined: Oct 23, 2006
Posts: 11

I was wondering whether there is a counterpart to ObjectOutputStream#writeChars (String str) in ObjectInputStream? Something like ObjectInputStream#readChars (), however I couldn't find such a method in this class.

So my question is, how can I read a serialized String written with writeChars from ObjectInputStream?


Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

writeChars() doesn't serialize a whole string; in particular, it doesn't write the length of the String. If you use this method, then you have to know that somehow (perhaps just by writing it to the stream first using writeInt().) Then to read the data in, you could read the int, create a char[], and then use a for loop to read each char and store it.

A lot easier to just use writeObject() or writeUTF().

[Jess in Action][AskingGoodQuestions]
It is sorta covered in the JavaRanch Style Guide.
subject: Counterpart to ObjecOutputStream#writeChars?
Similar Threads
transient variable ? is there any other way
Corresponding READ method for writeChars()
How to write a string in a file?
Object serialization writeChars()
Custom Serialization