File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes DataInputStream problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "DataInputStream problem" Watch "DataInputStream problem" New topic

DataInputStream problem

abhishek kunal
Ranch Hand

Joined: Jul 27, 2009
Posts: 42

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 what to do? please suggest.... thanks in advance!!
Naveen Ravindran

Joined: Jul 17, 2009
Posts: 1
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

Joined: Jul 27, 2009
Posts: 42
hey! thank you naveen, that solves my problem, but why that facility was not given in DataInputStream??
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

DataInputStream is for the binary formats of boolean et all. For textual representation, Scanner is a better solution.

How To Ask Questions How To Answer Questions
Sub swamy
Ranch Hand

Joined: Oct 02, 2002
Posts: 121
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.
I agree. Here's the link:
subject: DataInputStream problem
It's not a secret anymore!