• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DataInputStream problem

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i want to read a boolean and then read a byte(entered by the user), so what happens here is it reads the boolean and does'nt give the chance to enter the byte...perhaps it reads the existing stream as byte ..so what to do? please suggest.... thanks in advance!!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you read using the readBoolean of the DataInputStream, a value of 0 represents false and any other value represents true. This will basically read a byte of data from the Stream and give you a boolean based on the input. But these bytes are not removed from the Stream after the read. You can do this using the skip(long) method. This will skip a given number of bytes in the Stream. So if for the boolean input you had given 'f' you should skip by 2 but if you had given 'fa' you will have to skip by 4. This will let you enter inputs for the readByte(). But the readByte will again read a byte of data from the Stream and for me it was giving the ASCII value of the input(number) that I entered.

I would suggest using the Buffered reader for this functionality. It will not give you the readBoolean() and readByte() functions but you can use the readLine() and type cast the data and handle exceptions properly to achieve this functionality. I am attaching a sample code for the same, I have not added specific exception handling but you can always add it easily.

 
abhishek kunal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey! thank you naveen, that solves my problem, but why that facility was not given in DataInputStream??
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DataInputStream is for the binary formats of boolean et all. For textual representation, Scanner is a better solution.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a difference in the way in DataInputStream works in Java 6 and Java 1.4 ?

I have an application that makes a MQ call to fetch data from mainframe. While trying to port this application from WebSphere 6 (JDK 1.4) to WebSphere 7 (Java 6), what i observe is that the MQ call goes through fine, the message is received from mainframe. However, when the message is being parsed the runtime in WebSphere 7 raises an "ArrayIndexOutOfBoundsException". The flow is like

J2EE application (WebSphere 6) -> WebSphere MQ 5 -> Mainframe (IMS) - this flow works fine
J2EE application (WebSphere 7) -> WebSphere MQ 5 -> Mainframe (IMS) - this flow does not work and raises an exception.

There were no changes made to the application apart from having migrated to the new runtime - Java 6.
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic