• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Corresponding READ method for writeChars()

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a method to read chars from Input stream=>

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Get out of my mind! Look! A tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic