aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Corresponding READ method for writeChars() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Corresponding READ method for writeChars()" Watch "Corresponding READ method for writeChars()" New topic
Author

Corresponding READ method for writeChars()

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

I want to write a String "Hello World!" in a file using ObjectOutputStream and then read it back using ObjectInputStream and print it to screen.

I am using the writeChars() method of ObjectOutputStream. Here is a snippet of the code:


Two questions:
1. What corresponding method in FileInputStream can be used to read what was written using writeChars() ?
2. Is there a better way to code what I am trying to do?

Thanks,
Nidhi


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Nidhi Sar wrote:2. Is there a better way to code what I am trying to do?

Yes. Just use the writeObject() method to write an object and the readObject() method to read it back.
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Paul Clapham wrote:Just use the writeObject() method to write an object and the readObject() method to read it back.


Thanks Paul, that did work!

Do you also happen to know the answer to the first part of my question: if writeChars() is available as a method, there must be some way we should be able to read what is written. Is there?

Thanks,
Nidhi
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Yes. Just use the writeObject() method to write an object and the readObject() meth


Paul, I think that will be serialization.

there is a readchar() method I think you can use that.


SCJP 1.6 96%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

There is a method to read chars from Input stream=>


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

but it will read 1 char each time.
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Minhaj kaimkhani wrote:but it will read 1 char each time.


Right, readChar() would read just one char at a time. I was looking for a readChars(). But there is no such method. I was just wondering that without such a method, how would you read something(a String) written with writeChars() ?
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400


Writes every character in the string s, to the output stream, in order, two bytes per character. If s is null, a NullPointerException is thrown. If s.length is zero, then no characters are written. Otherwise, the character s[0] is written first, then s[1], and so on; the last character written is s[s.length-1]. For each character, two bytes are actually written, high-order byte first, in exactly the manner of the writeChar method.

http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataOutput.html#writeChars%28java.lang.String%29

see, when you call writeChars(str) it will also write one by one! neither you can read all chars at once nor read!

hope its clear to you?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Neha Daga wrote:
Yes. Just use the writeObject() method to write an object and the readObject() meth
Paul, I think that will be serialization.

Of course it will be serialization. There is no reason to use an ObjectOutputStream if you don't plan to use serialization. If you just want to write primitives and Strings, then a DataOutputStream is a better choice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Corresponding READ method for writeChars()