Runner Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
package com.gaurangjadia.code.java; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Date; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.UIManager; public class Runner extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; public String strPath = ""; private ObjectInputStream oisData; public Runner() { JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Select Directory to Save File"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setAcceptAllFileFilterUsed(false); int intAction = chooser.showOpenDialog(this); if (intAction == JFileChooser.APPROVE_OPTION) { File f = chooser.getSelectedFile(); strPath = f.getAbsolutePath(); Example objExampleWrite = new Example("This is Awesome", "This is Great", new Date()); try { //Write Object FileOutputStream fosData = new FileOutputStream(strPath + "//Example.data"); ObjectOutputStream oosData = new ObjectOutputStream(fosData); oosData.writeObject(objExampleWrite); oosData.close(); fosData.close(); //Read Object FileInputStream fisData = new FileInputStream(strPath + "//Example.data"); oisData = new ObjectInputStream(fisData); Example objExampleRead = (Example) oisData.readObject(); System.out.print(objExampleRead); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { try { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } run(new Runner(), 250, 110); } public static void run(JFrame frame, int width, int height) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(width, height); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { System.out.println(e); } } |
Example Class to Serialize
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.gaurangjadia.code.java; import java.io.Serializable; import java.util.Date; public class Example implements Serializable { private static final long serialVersionUID = 1L; private String title; private String description; private Date date; public Example(String strTitle, String strDescription, Date dtDate) { this.setTitle(strTitle); this.setDescription(strDescription); this.setDate(dtDate); } @SuppressWarnings("deprecation") public String toString() { this.getDate(); return this.getTitle() + " " + this.getDescription() + " " + this.getDate().getYear(); } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } } |