简体   繁体   中英

Java: How to store entire object to an external location and reuse it to recreate the object for later use?

I'm looking for a way to store an instantiated object and all of its properties to an external location and reuse it later.

What I tried so far:
I came across this [Serialization?] answer and tried it but the object I'm getting is null.

Algo / Rough code:

@Listeners(TestListener.class)
public class TestRunner {
    protected static Driver driver; //Driver implements WebDriver interface

    //Step 1
    public void method1(){
    driver = new Driver(1); //creates a customized chromedriver instance. Chrome driver / browser session is opened here.
    }

    //Step 2
    public void method2(){
        try {
            FileOutputStream fileOut = new FileOutputStream("card.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(driver); 
            out.close();
            fileOut.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //Step 3
    //Just manually stop the test/debugger for testing

    //Step 4
    public void method4(){
        try {
            FileInputStream fileIn = new FileInputStream("card.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            driver = (Driver) in.readObject(); //here driver returns as null. In this line, im hoping to recreate the previous object to somehow control the browser again
            in.close();
            fileIn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Some info:
This is actually related to a Selenium WebDriver problem in this SO question.

So what I'm trying to do is:

  • Step 1: I create a customized instance of a chrome driver
  • Step 2: Store the created object into an external source using the above code
  • Step 3: I intentionally stop the test to simulate failure
  • Step 4: I try to re-create the previous object by reading the stored source.

    I don't have prior background with serialization and I'm not sure if what I'm trying to do is possible. Any help or guidance is much appreciated.

  • Assuming that Driver class also implements Serializable, one problem i see in your code is that driver is declared as protected static Driver driver, but method 1 which is suppose to create an instance of this class never gets executed, so you don't have that object when you serialise it. try calling method 1 before serialising it like this:

     public void method2(){
        try {
            FileOutputStream fileOut = new FileOutputStream("card.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            method1();
            out.writeObject(driver); 
            out.close();
            fileOut.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    

    Although still incomplete, I managed to at least store the object using JAXB-MOXy. I have decided I'll be using this solution towards my problem as I'm having troubles using ObjectOutputStream mainly due to the objects used on the class I'm trying to export are 3rd party and does not implement Serializable.

    For those who will be facing a similar problem, you might find the following links helpful:

  • Make your JAXB cleaner with the MOXy implementation
  • Handling interfaces in JAXB with MOXy

  • The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM