I'm starting to learn Java I/O. I have a binary File with Integer data in it and I'm trying to read from it using FileInputStream and showing the content of the file on the console. It seems I'm not making the right approach, keep getting -1 witch means end of file. Here is my code.
File f=new File("sunday.dat");
FileInputStream fis=new FileInputStream(f);
try {
while (true) {
number=fis.read();
System.out.println(number);
}
} catch (EOFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fis.close();
while (true) {...}will try to execute its code block infinitely. If you don't have any stop condition (or break/return/throw statements) then-1will be printed all the time after you read file content. You need to rethink your loop. BTW if you want to read data as text useReader.Streamsare for binary data.