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();
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.
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