package class7_2_LoadingObjectData_inClass;// Dr. Yoder. MSOE. 26 January 2017 import java.io.*; import java.nio.file.Paths; public class Circle { //implements Serializable { private int centerY; private int centerX; private int radius; public Circle(int centerY, int centerX, int radius) { this.centerY = centerY; this.centerX = centerX; this.radius = radius; } public static Circle read(String filename) throws ShapeException { try { FileInputStream fileInStream = new FileInputStream(Paths.get(filename).toFile()); ObjectInputStream dataIn = new ObjectInputStream(fileInStream); // centerX = dataIn.readShort(); // centerY = dataIn.readShort(); // radius = dataIn.readShort(); Circle other = (Circle) dataIn.readObject(); fileInStream.close(); return other; } catch (EOFException e) { String message = "We reached the end of the file before reading in the whole circle object."; System.out.println(message); throw new ShapeException(message,e); } catch (FileNotFoundException e) { String message = "Could not find the file: "+filename; System.out.println(message); throw new ShapeException(message,e); } catch (IOException e) { String message = "An unknown file IO exception occured. "+e.getMessage(); System.out.println(message); throw new ShapeException(message,e); } catch (ClassNotFoundException e) { throw new ShapeException("Could not find the class for the object found in the file",e); } } public void write(String filename) throws ShapeException { try { FileOutputStream fileOutStream = new FileOutputStream(Paths.get(filename).toFile()); ObjectOutputStream objectOut = new ObjectOutputStream(fileOutStream); // objectOut.writeShort(centerX ); // objectOut.writeShort(centerY); // objectOut.writeShort(radius); objectOut.writeObject(this); objectOut.close(); fileOutStream.close(); } catch (EOFException e) { String message = "We reached the end of the file before reading in the whole circle object."; System.out.println(message); throw new ShapeException(message,e); } catch (FileNotFoundException e) { String message = "Could not find the file: "+filename; System.out.println(message); throw new ShapeException(message,e); } catch (IOException e) { String message = "An unknown file IO exception occured. "+e.getMessage(); System.out.println(message); throw new ShapeException(message,e); } } @Override public String toString() { return "Circle at ("+centerX+", "+centerY+") with radius "+radius; } }