Java 17 Recipes
-1. Serializing Java Objects
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
8-1. Serializing Java Objects
Problem You need to serialize a class (save the contents of the class) so that you can restore it at a later time. Solution Java implements a built-in serialization mechanism. You access that mechanism via the ObjectOutputStream class. In the following example, the saveSettings() method uses ObjectOutputStream to serialize the settings object to write the object to disk. public class Ch_8_1_SerializeExample { public static void main(String[] args) { Ch_8_1_SerializeExample example = new Ch_8_1_SerializeExample(); example.start(); } private void start() { ChApTer 8 InpuT And OuTpuT 295 ProgramSettings settings = new ProgramSettings(new Point(10,10), new Dimension (300,200), Color.blue, "The title of the application" ); saveSettings(settings,"settings.bin"); ProgramSettings loadedSettings = loadSettings("settings.bin"); if(loadedSettings != null) System.out.println("Are settings are equal? :"+loadedSettings. equals(settings)); } private void saveSettings(ProgramSettings settings, String filename) { try { FileOutputStream fos = new FileOutputStream(filename); try (ObjectOutputStream oos = new ObjectOutputStream(fos)) { oos.writeObject(settings); } } catch (IOException e) { e.printStackTrace(); } } private ProgramSettings loadSettings(String filename) { try { FileInputStream fis = new FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(fis); return (ProgramSettings) ois.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return null; } } ChApTer 8 InpuT And OuTpuT 296 The following is the ProgramSetting class. package org.java17recipes.chapter08.recipe08_01; import java.awt.*; import java.io.Serializable; public class ProgramSettings implements Serializable { private static final long serialVersionUID = 1L; private Point locationOnScreen; private Dimension frameSize; private Color defaultFontColor; private String title; // Empty constructor public ProgramSettings() { } public ProgramSettings(Point locationOnScreen, Dimension frameSize, Color defaultFontColor, String title) { this.locationOnScreen = locationOnScreen; this.frameSize = frameSize; this.defaultFontColor = defaultFontColor; this.title = title; } public Point getLocationOnScreen() { return locationOnScreen; } public void setLocationOnScreen(Point locationOnScreen) { this.locationOnScreen = locationOnScreen; } public Dimension getFrameSize() { return frameSize; } ChApTer 8 InpuT And OuTpuT 297 public void setFrameSize(Dimension frameSize) { this.frameSize = frameSize; } public Color getDefaultFontColor() { return defaultFontColor; } public void setDefaultFontColor(Color defaultFontColor) { this.defaultFontColor = defaultFontColor; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ProgramSettings that = (ProgramSettings) o; if (defaultFontColor != null ? !defaultFontColor. equals(that.defaultFontColor) : that.defaultFontColor != null) return false; if (frameSize != null ? !frameSize.equals(that.frameSize) : that.frameSize != null) return false; if (locationOnScreen != null ? !locationOnScreen. equals(that.locationOnScreen) : that.locationOnScreen != null) return false; ChApTer 8 InpuT And OuTpuT 298 if (title != null ? !title.equals(that.title) : that.title != null) return false; return true; } @Override public int hashCode() { int result = locationOnScreen != null ? locationOnScreen. hashCode() : 0; result = 31 * result + (frameSize != null ? frameSize. hashCode() : 0); result = 31 * result + (defaultFontColor != null ? defaultFontColor.hashCode() : 0); result = 31 * result + (title != null ? title. hashCode() : 0); return result; } } Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling