Save Jcodemodel Object after exit

165 views Asked by At

I have a issue with JCodeModel (SUN). My program is running every day, and I want to add some function to classes which was created before the current running.

JcodeModel support this? If not, there is any option to save the JCodemodel Object in external file, load the previous JcodeModel, and then add the new functions?

Thanks.

1

There are 1 answers

2
Jan Galinski On

You could save the instance to file using ObjectOutputStream and then read and instatiate it with ObjectInputStream. As long as you control the system and can ensure that the version does not change overnight, this should be safe (though unusual).

This tutorial demonstrates how to use it:

import java.io.*;
public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
     String s = "Hello world!";
     int i = 897648764;
     try {

       // create a new file with an ObjectOutputStream
       FileOutputStream out = new FileOutputStream("test.txt");
       ObjectOutputStream oout = new ObjectOutputStream(out);

       // write something in the file
       oout.writeObject(s);
       oout.writeObject(i);

       // close the stream
       oout.close();

       // create an ObjectInputStream for the file we created before
       ObjectInputStream ois =
             new ObjectInputStream(new FileInputStream("test.txt"));

       // read and print what we wrote before
       System.out.println("" + (String) ois.readObject());
       System.out.println("" + ois.readObject());

  } catch (Exception ex) {
     ex.printStackTrace();
  }
}
}