I am trying to read one byte integers from a file. The integers are read correctly however the process keeps reading the integer '-1' without throwing an EOF exception.
This is how I write the integers into the file. I read 4 byte integers from class1.dat and put those which are below 256 in output1.dat as 1 byte integers.
FileInputStream fis = new FileInputStream("D:\\class1.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
FileOutputStream fos = new FileOutputStream("D:\\output1.dat");
DataOutputStream dos = new DataOutputStream(fos);
try
{
while(true)
{
number = dis.readInt();
System.out.println("Integer read : " + number);
if(number < 256)
dos.write(number);
}
}
catch(EOFException e)
{
dis.close();
dos.close();
}
This is how i read the 1 byte integers from output1.dat.
DataInputStream dis = new DataInputStream(new FileInputStream("D:\\output1.dat"));
try
{
int number;
System.out.println("File with < 256");
while(true)
{
number = dis.read();
System.out.println("Number = " + number);
}
}
catch(EOFException e)
{
dis.close();
}
Why doesn't EOFexception get thrown?