problem in reading integers data

hi all,

i'm reading integers from console and storing them in a file and then reading back, but my data is inconsistent.can anybody tell me the reason for this.

heres my code snippet...

DataInputStream in = new DataInputStream(System.in);

DataOutputStream out = new DataOutputStream(new FileOutputStream("intdata.txt"));

try{

int a = in.readInt();

out.writeInt(a);

out.close();

in.close();

}catch(EOFException eofe){

throw new RuntimeException(eofe);

}

DataInputStream in2 = new DataInputStream(new FileInputStream("intdata.txt"));

System.out.println(in2.readInt());

in2.close();

[677 byte] By [gaurav_abbia] at [2007-11-27 3:33:08]
# 1
So what are you typing as input? I'm pretty sure the DIS correctly converts whatever you type to an int value (probably the char values you write), and then lets the DOS write those to the file. Broken as designed.
CeciNEstPasUnProgrammeura at 2007-7-12 8:36:12 > top of Java-index,Java Essentials,Java Programming...
# 2

hi,

i gave 58 and the data it displayed from file is 892865802

below is what i got on console...

D:\gaurav\java\MyLearning\AdvancedJava\AdvanceJavaPractice>java ioPractice.ReadInt

58

892865802

D:\gaurav\java\MyLearning\AdvancedJava\AdvanceJavaPractice>

gaurav_abbia at 2007-7-12 8:36:12 > top of Java-index,Java Essentials,Java Programming...
# 3

Did you understand reply 1? Particularly the bit about how the DataInputStream will convert the String you enter at the keyboard ("58") into some int or other.

Try this:DataInputStream in = new DataInputStream(System.in);

DataOutputStream out = new DataOutputStream(new FileOutputStream("intdata.txt"));

try{

int a = in.readInt();

/*

* Add this line to see what int the DataInputStream

* is creating based on your "58"

*/

System.out.println("The number I typed in was " + a);

out.writeInt(a);

out.close();

in.close();

} catch(EOFException eofe) {

throw new RuntimeException(eofe);

}

DataInputStream in2 = new DataInputStream(new FileInputStream("intdata.txt"));

System.out.println(in2.readInt());

in2.close();

[Edit] Ceci believed you! And so did I!

You didn't get the output you said you did when you entered 58 at the console and had a DataInputStream try and interpret that as as an int. I know because I've just compiled what you posted and tried it.

pbrockway2a at 2007-7-12 8:36:12 > top of Java-index,Java Essentials,Java Programming...
# 4
ok,i'm getting what you are saying, but is there a way to achieve what i'm trying to, apart from parseInt, because System.in is an inputStrream and my DataInputStream wraps over that which has a readInt() function which should function exactly as i'm expecting..
gaurav_abbia at 2007-7-12 8:36:12 > top of Java-index,Java Essentials,Java Programming...
# 5

The whole problem here is in that last phrase "a readInt() function which should function exactly as i'm expecting." What it actually does do in the posted code is sit there doing nothing, or rather it waits for more input. It's doing exactly what its API documentation says it will do.

But you say it "should function exactly as i'm expecting." Now unless you pay Sun a very, very big sum of money DataInputStreams are going to go on behaving as documented in the API. That isn't going to change. You had better change what you are expecting.

If you throw a String like "58" at a DataInputStream readInt() will not produce 58. (And nowhere in the documentation of the class does it suggest it will.) What it will do - and I've tried this - is sit there and do nothing. This was sort of what Ceci was getting at by "Broken As Designed". It's not that DIS is broken, after in2 (see below) works fine. But using it to read keyboard input is a Bad Thing.

Having changed what we expect, we need to rethink that "apart from parseInt" bit. We need a plan B, and here it is. We are reading a string (a bunch of characters such as people produce when they hammer a keyboard) - so we'll use a Reader. We want an entire line, so we'll make it a BufferedReader and use its readline() method. And we want to obtain an int from that string, which pretty much suggests ... parseInt().

At any rate, that's my suggestion! Something along the lines of:import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.EOFException;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStreamReader;

public class Test {

public static void main(String[] args) throws Exception {

//DataInputStream in = new DataInputStream(System.in);

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

DataOutputStream out = new DataOutputStream(new FileOutputStream("intdata.txt"));

try{

//int a = in.readInt();

int a = Integer.parseInt(in.readLine());

/*

* Add this line to see what int the DataInputStream

* is creating based on your "85"

*/

System.out.println("The number I typed in was " + a);

out.writeInt(a);

out.close();

//in.close();

} catch(EOFException eofe) {

throw new RuntimeException(eofe);

}

DataInputStream in2 = new DataInputStream(new FileInputStream("intdata.txt"));

System.out.println(in2.readInt());

in2.close();

}

}

Which results in58

The number I typed in was 58

58

pbrockway2a at 2007-7-12 8:36:12 > top of Java-index,Java Essentials,Java Programming...