Java Reference
In-Depth Information
private class ListItem implements Serializable {
// Inner class definition as before...
}
}
Of course, each file must also have an import statement for the java.io.Serializable class as in
the code above.
The modified version of the TryPolyLine class to write the PolyLine objects to a stream looks like this:
import java.io.*;
public class TryPolyLine {
public static void main(String[] args) {
// Create an array of coordinate pairs
double[][] coords = { {1., 1.}, {1., 2.}, { 2., 3.},
{-3., 5.}, {-5., 1.}, {0., 0.} };
// Create a polyline from the coordinates and display it
PolyLine polygon = new PolyLine(coords);
System.out.println(polygon);
// Add a point and display the polyline again
polygon.addPoint(10., 10.);
System.out.println(polygon);
// Create Point objects from the coordinate array
Point[] points = new Point[coords.length];
for(int i = 0; i < points.length; i++)
points[i] = new Point(coords[i][0],coords[i][1]);
// Use the points to create a new polyline and display it
PolyLine newPoly = new PolyLine(points);
System.out.println(newPoly);
// Write both polyline objects to the file
try {
// Create the object output stream
ObjectOutputStream objectOut =
new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream("C:/Beg Java Stuff/Polygons.bin")));
objectOut.writeObject(polygon); // Write first object
objectOut.writeObject(newPoly); // Write second object
objectOut.close(); // Close the output stream
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
Search WWH ::




Custom Search