Java Reference
In-Depth Information
out.writeDouble(d);
out.close();
}
public static double[] readData(String file)
throws IOException
{
InputStream fin = new FileInputStream(file);
DataInputStream in = new DataInputStream(fin);
double[] data = new double[in.readInt()];
for (int i = 0; i < data.length; i++)
data[i] = in.readDouble();
in.close();
return data;
}
The
writeData
method first opens the file and writes the array length. It
then loops, writing the contents of the array. The file can be read into an
array with
readData
. These methods can be rewritten more simply using
the contents of an object to a
DataOutputStream
and add a constructor
that will read the state from a
DataInputStream
.