So I’m a Java-newbie and started some playing with files. Say I have some file “tes.t” containing data of a type known for me – assume they’re int-double-int-double and so forth. I don’t know the amount of such pairs inside, though – how can I make sure the input has finished? For my current knowledge, I thought of something like this:
try{
DataInputStream reading = new DataInputStream(new FileInputStream("tes.t"));
while(true)
{
System.out.println(reading.readInt());
System.out.println(reading.readDouble());
}
}catch(IOException xxx){}
}
However, this infinite loop here makes me somehow uncomfortable. I mean – I guess the IOException should catch on as soon as the input has finished but I’m not sure if that’s a good way to go. Is there any better way to do this? Or rather – what is a better approach as I’m sure mine is bad 🙂
Since your file has int-double pair, You can do that as following: