• 6 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/15/20

Persist Data Within Your Java Applications

Evaluated skills

  • Persist data within your Java applications
  • Question 1

    What does serializing mean?

    • Storing things.

    • Doing one thing after the other.

    • Doing lots of things at once.

    • Eating a corn-based breakfast with some milk and a cup of coffee.

  • Question 2

    Have a look at the following code examples that persists data using the properties class:

    public class Vehicle {
        public String registration;
        public String make;
        public String model;
        public int weightKg;
    }
    
        public void serialiseVehicle(Vehicle v) throws IOException {
            Properties p = new Properties();
    
            p.setProperty("registration", v.registration);
            p.setProperty("make", v.make);
            p.setProperty("model", v.model);
    
            FileWriter fw = new FileWriter("Store.txt");
            p.store(fw, "Vehicle");
            fw.close();
        }
    
        public Vehicle deserialiseVehicle() throws IOException {
             Properties p = new Properties();
             FileReader fr = new FileReader("Store.txt"); 
             p.load(fr);
             fr.close();
    
             Vehicle v = new Vehicle();
             v.registration = p.getProperty("Registration");
             v.model = p.getProperty("model");
             v.make = p.getProperty("make");
             v.weightKg = p.getProperty("weight");
    
            return person;  
        }
    
    

    Check the boxes for all the correct statements about this code.

    Careful, there are several correct answers.
    • The deserialized  Vehicle  won't contain the same values as the serialized  vehicle  because property keys must be spelled the same - and use the same case - when writing and reading.

    • The deserialized  vehicle  will fail to load because the properties are read in the wrong order compared to the order they were written.

    • The code will fail because it tries to read a property that wasn't written.

    • The code will fail because the properties are stored as strings and not all the  Vehicle  fields are strings.

  • Question 3

    Which one of the following annotations would be the best option to tell JAXB that you want to: 

    • Store the class RobotPart.
    • Require the Robot Part to have a name?
    • @JaxbElement
      public class RobotPart {
      
          @JaxbProperty(required=true)
          public String name;
      
          @JaxbProperty
          public Float price;
          
          @JaxbProperty
          public Float weight;
          
          @JaxbProperty
          public String description;
      }
      
    • @XmlElement
      public class RobotPart {
      
          @XmlElement(null=false)
          public String name;
      
          public Float price;
          
          public Float weight;
          
          public String description;
      }
      
    • @XmlRootElement
      public class RobotPart {
      
          @XmlElement(required=true)
          public String name;
      
          public Float price;
          public Float weight;
          public String description;
      }
      

       

    • @XmlElement
      public class RobotPart {
      
          @XmlProperty(required=true)
          public String name;
      
          @XmlProperty
          public Float price;
          
          @XmlProperty
          public Float weight;
          
          @XmlProperty
          public String description;
      }
      
Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best