Java code for storing object streams

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.