reading a from a binary file
Hey, iv been trying to read a binary file in java but i seem to have problems reading all the numbers any help would be great, i have tested the code below using print lines to find what's wrong and it seems that although it tells me it has reached the end of the file its only giving me the very first value of the item saved in the file, so it seems to be some sort of fundamental looping problem :
Code:
//this is the function i am using to save 8 integer values to a file
public void export_file() throws IOException
{
File file = null;
//create a new file
if (file == null)
{
file = new File("Project.mxi");
}
try
{
FileOutputStream f_out = new FileOutputStream (file);
DataOutputStream d_out = new DataOutputStream (f_out);
for (int i=0;i <= 8; i++)
{
//only do it for the first item not 0
if (i > 0)
{
int x;
//x = value from array list
x = listofvalues.get(i);
d_out.writeInt(x);
}
}
f_out.close();
//close the file from being read
}
catch (IOException e)
{
}
}
public void read_file()
{
File file = null;
int x;
boolean exists = (new File("Project.mxi")).exists();
if (exists)
{
file = new File("Project.mxi");
}
else
{
System.out.println("File is corrupt or unreadable");
}
try
{
FileInputStream f_in = new FileInputStream(file);
DataInputStream d_in = new DataInputStream (f_in);
while(true)
{
try
{
x = d_in.readInt();
}
catch(EOFException eof)
{
System.out.println("end of file");
break;
}
d_in.close();
}
}
catch(IOException e)
{
}
}
Any help would be fantastic :)