4 Replies Latest reply: Nov 15, 2010 2:15 AM by 814176 RSS

    Array to Binary File

    814176
      Hi guys
      I am trying to save an array into a binary file. I am using the following code for saving
      ******************
      Employee[] empArr=new Employee[2];
      empArr[0]=new Employee("John", "Smith", "12345", "61111111", 120000, 5);
      try{
      ObjectOutputStream outputStream =
      new ObjectOutputStream(
      new FileOutputStream("array.file"));
      outputStream.writeObject(empArr);
      outputStream.close();
      JOptionPane.showMessageDialog(null, "Saving Complete!");
      }
      catch(IOException e){
      JOptionPane.showMessageDialog(null, "An error occurred!");
      }
      ************************************************************************
      The problem is when I use the code above an the IOException rise and the array is not saved into the file.
      ************************************************************************
      The definition of the Employee class:
      package project4;
      import java.util.Scanner;
      import java.io.Serializable;
      public class Employee implements Serializable {
      private String firstname;
      private String lastname;
      private String id;
      private String phone;
      private double yearlySalary;
      private double monthlyPay;
      private double bounus;
      private Scanner keyboard=new Scanner(System.in);

      public Employee(){
      keyboard.useDelimiter("\n");
      }
      public Employee(String FirstName, String LastName, String ID, String Phone, double YearlySalary,double Bonus ){

      this.firstname=FirstName;
      this.lastname=LastName;
      this.id=ID;
      this.phone=Phone;
      this.yearlySalary=YearlySalary;
      this.bounus=Bonus;
      this.monthlyPay=(this.yearlySalary/12)+((this.yearlySalary/12)*(bounus/100));
      keyboard.useDelimiter("\n");
      }

      public void setLastname(String ln ){
      lastname=ln;
      }

      public String getLastname(){
      return lastname;
      }

      public void setFirstname(String ln ){
      firstname=ln;
      }

      public String getFirstname(){
      return firstname;
      }

      public boolean inputLastname(){
      try {
      System.out.print("Please enter employee last name: ");
      lastname=keyboard.next();
      return true;
      } catch (Exception e){
      return false;
      }
      }

      public void inputfirstname(){
      System.out.print("Please enter employee first name: ");
      firstname=keyboard.next();
      }

      public void setID(String id){
      this.id=id;
      }

      public String getID(){
      return this.id;
      }

      public void inputID(){
      System.out.print("Please enter employee ID: ");
      this.id=keyboard.next();
      }

      public void setPhone(String phone){
      this.phone=phone;
      }

      public String getPhone(){
      return this.phone;
      }

      public void inputPhone(){
      System.out.print("Please enter employee phone number: ");
      this.phone=keyboard.next();
      }

      public void setYearlySalary(double ys){
      this.yearlySalary=ys;
      }

      public double getYearlySalary(){
      return this.yearlySalary;
      }

      public void inputYearlySalary(){
      System.out.print("Please enter employee yearly salary: ");
      this.yearlySalary=keyboard.nextDouble();
      }

      public void calcBonus(double perecent){
      this.bounus=yearlySalary*(perecent/100);
      }

      public double getBonus(){
      return this.bounus;
      }
      public void setBonus( double bonus){
      this.bounus=bonus;

      }


      public void calcMonthlyPay(){
      this.monthlyPay=(this.yearlySalary/12)+((this.yearlySalary/12)*(bounus/100));
      }

      public double getMonthlyPay(){
      return this.monthlyPay;
      }

      public boolean inputAll(){
      boolean b;
      this.inputfirstname();
      b=this.inputLastname();
      this.inputID();
      this.inputPhone();
      this.inputYearlySalary();
      return b;
      }

      public String toString(){
      return "Name: "+lastname+","+firstname+"\n"+"ID: "+id+"\n"+"Phone: "+phone+"\n"+"Salary: $"+Double.toString(yearlySalary)+"\n"+"Bonus: $"+Double.toString(bounus)+"\n"+"Monthly Pay: $"+Double.toString(monthlyPay)+" "+"\n";
      }

      public int compareTo(Object obj){
      Employee e2=(Employee)obj;
      if ((this.firstname+" "+this.lastname).compareTo(e2.firstname+" "+e2.lastname)==0)
      return 0;
      else if ((this.firstname+" "+this.lastname).compareTo(e2.firstname+" "+e2.lastname)>0)
      return 1;
      else
      return -1;
      }

      }
      ******************