Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Corresponding READ method for writeChars()

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a method to read chars from Input stream=>

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it will read 1 char each time.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic