Got a question about reading and writing binary files.
I know that to write a value(integer) to a binary file I can use the fileoutputstrea m to do it.
But I cant seem to read back from the file these values using the fileinputstream
Any idea where I went wrong? Thanks in advance :)
I know that to write a value(integer) to a binary file I can use the fileoutputstrea m to do it.
Code:
try
{
// Create an output stream to the file.
FileOutputStream file_output = new FileOutputStream (outFile);
//for(int i = 0; i < cInt.length; i++)
file_output.write(100);
file_output.write(200);
file_output.write(300);
// Close file when finished with it..
file_output.close ();
}
Code:
int bInt = 0;
try
{
// Wrap the FileInputStream with a DataInputStream
FileInputStream file_input = new FileInputStream (inFile);
DataInputStream data_in = new DataInputStream (file_input);
while(true)
{
try
{
bInt = data_in.readInt();
}
catch (EOFException eof)
{
//end of file
break;
}
System.out.println("Test: "+bInt);
}
data_in.close ();
}
catch(IOException e)
{
System.out.println ( "IO Exception =: " + e );
System.exit(0);
}
Comment