The first time I saw Fang Shen use an object stream to store information that has already been executed, I feel that this operation is really amazing. Through the object stream, I can save dynamic objects.
For example, if half of the clearance program is executed, you suddenly don't want to clear the mine. Then, after storing the JPanel object (both the Clearance Panel) to the file with the object stream, then you can use an object stream to read data.
In the process of implementing mine clearance, I suddenly found that I had read operations in the object stream, and then the panel did not respond, considering the need to develop the LAN combat function, we plan to delete the Save progress module.
But the implementation code is put here.
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;
The above is the class to be included
If (e. getSource (). equals (win. menuBar. backUp) {// The response File file = new File ("save.txt") in the SAVE box; try {if (file. exists () = false) file. createNewFile (); else {file. delete (); file. createNewFile ();} FileOutputStream fileOut = new FileOutputStream (file); ObjectOutputStream out = new ObjectOutputStream (fileOut); out. writeObject (win. mainArea); out. close ();} catch (Exception ss) {System. out. println (ss. toString (); JOptionPane. showMessageDialog (win, "Save error", "Warning box", JOptionPane. WARNING_MESSAGE) ;}} if (e. getSource (). equals (win. menuBar. recover) {// The response File file = new File ("save.txt"); try {if (file. exists () = false) throw new Exception (); FileInputStream fileIn = new FileInputStream (file); ObjectInputStream in = new ObjectInputStream (fileIn); win. setMenuArea (MenuArea) in. readObject (); in. close ();} catch (Exception ss) {JOptionPane. showMessageDialog (win, "read error", "Warning box", JOptionPane. WARNING_MESSAGE );}}
It cannot be appropriate to merge the time count function (and re-open a process to execute the operation), and the normal response operation cannot be performed after the results are read from the archive.