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 Object streams you will learn about in Section 20.8 on page 549 .
Exercise 20.7 : Add a method to the Attr class of Chapter 3 that writes
the contents of an object to a DataOutputStream and add a constructor
that will read the state from a DataInputStream .
 
Search WWH ::




Custom Search