daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class SerializationTest {
  2.    
  3.     public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
  4.        
  5.          
  6.          File inputFile = new File("test.txt"); // The file to store and retrieve
  7.          FileOutputStream fileStream = new FileOutputStream(inputFile); // File output stream
  8.          ObjectOutputStream objectStream = new ObjectOutputStream(fileStream); // Object output stream
  9.          
  10.          Car bmw = new Car("BMW", 2015, 4);
  11.          objectStream.writeObject(bmw);   // Writing object into file
  12.          objectStream.close();            // Closing the object stream
  13.          
  14.          FileInputStream inFile = new FileInputStream(inputFile);
  15.          ObjectInputStream objectIn = new ObjectInputStream(inFile);
  16.          
  17.          Car bmw_retrive = (Car) objectIn.readObject();
  18.          System.out.println("Car Model       : " + bmw_retrive.getCarModel());
  19.          System.out.println("Made in Year of : " + bmw_retrive.getYearMade());
  20.          System.out.println("Number of Tires : " + bmw_retrive.numberOfTires);
  21.          System.out.println("Quantity        : " + Car.quantity);
  22.          
  23.          objectIn.close();  // closing the object stream
  24.          
  25.        
  26.     }
  27.    
  28. }
  29.  
  30. class Car implements Serializable{
  31.     private String carModel;
  32.     static int quantity;
  33.     public final int numberOfTires;
  34.     private int yearMade;
  35.     public Car(String carModel, int yearMade, int numOfTires){
  36.         this.carModel = carModel;
  37.         this.yearMade = yearMade;
  38.         this.numberOfTires = numOfTires;
  39.     }
  40.     public String getCarModel(){return carModel;}
  41.     public int getYearMade(){return yearMade;}
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top