0

I want to Save my Full LibraryDatabase object in "DB.bin". When I add an new record in my object it added successfully but when i exit it. And then Restart the Program then It can't find any record. Though These classes are already Serialize but I can't understand why it does not work. There is any wrong in code? How to fix it ?

Data.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.Serializable;

/**
 *
 * @author sohan
 */
public class Data implements Serializable{
/**
     * 
     */
    private static final long serialVersionUID = 1L;
String stu_name,stu_id, stu_cont,book_name,writter_name;

    public Data(String stu_name, String stu_id, String stu_cont, String book_name, String writter_name) {
        this.stu_name = stu_name;
        this.stu_id = stu_id;
        this.stu_cont = stu_cont;
        this.book_name = book_name;
        this.writter_name = writter_name;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public String getStu_id() {
        return stu_id;
    }

    public void setStu_id(String stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_cont() {
        return stu_cont;
    }

    public void setStu_cont(String stu_cont) {
        this.stu_cont = stu_cont;
    }

    public String getBook_name() {
        return book_name;
    }

    public void setBook_name(String book_name) {
        this.book_name = book_name;
    }

    public String getWritter_name() {
        return writter_name;
    }

    public void setWritter_name(String writter_name) {
        this.writter_name = writter_name;
    }

    @Override
    public String toString() {
        return "Student Name: " + stu_name + ", Student Id: " + stu_id + ", Student Contact: " + stu_cont + ", Book Name: " + book_name + ", Writter Name: " + writter_name;
    }

}

LibraryDatabase.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.Serializable;
import java.util.*;
/**
 *
 * @author sohan
 */
public class LibraryDatabase implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static Data[] data_=new Data[100];
    /**
     * @param args the command line arguments
     */ 
    public static int lastindex()
    {
        for(int i=0;i<data_.length;i++)
        {
            if (data_[i]==null)
            {
                return i;
            }
        }
        return 0;
    }


    public static void AddData(String s_name,String s_id,String s_phone, String b_name, String w_name)
    {
        Data new_data= new Data(s_name,s_id,s_phone,b_name,w_name);
        data_[lastindex()]=new_data;

    }

    public static void view_data()
    {
        for (int i = 0; i < data_.length; i++) 
        {
            if(data_[i]!=null)
            System.out.println(data_[i].toString());
            else {
                break;
            }
        }

    }

}

Demo.java:(Main Method in here)

import java.io.*;
import java.util.*;


public class Demo {

   public static void main(String [] args) {
       FileInputStream fileIn=null;;
       ObjectInputStream in = null;
       Scanner scan = new Scanner(System.in);
      LibraryDatabase  e = null;
      try {
         fileIn = new FileInputStream("DB.bin");
         in = new ObjectInputStream(fileIn);
         e = (LibraryDatabase) in.readObject();
         in.close();
         fileIn.close();
        } catch (Exception en) {
          System.out.println("Not Found");
          e=new LibraryDatabase();
        }

      while (true)
      {
          int n;
          System.out.println("1.Add Data");
          System.out.println("2.View Data");
          System.out.println("3.Exit");
          n=scan.nextInt();
          if(n==1)
          {
              String stu_name,stu_id, stu_cont,book_name,writter_name;
              stu_name=scan.next();
              stu_id=scan.next();
              stu_cont= scan.next();
              book_name=scan.next();
              writter_name=scan.next();
              e.AddData(stu_name,stu_id, stu_cont,book_name,writter_name);
          }
          if(n==2)
          {
              e.view_data();
          }
          if(n==3)
          {
              try {
                     FileOutputStream fileOut =
                     new FileOutputStream("DB.bin");
                     ObjectOutputStream out = new ObjectOutputStream(fileOut);
                     out.writeObject(e);
                     out.flush();
                     out.close();
                     fileOut.close();
                     System.out.printf("Serialized data is saved in DB.bin");
                  } catch (IOException i) {
                     i.printStackTrace();
                  }
              System.exit(0);
          }
      } 
   }            
}
3
  • Do you see an exception anywhere? Does the DB.bin file exist and has non-zero size? Commented May 26, 2020 at 14:49
  • No. I don't see any exception Commented May 26, 2020 at 15:00
  • What exactly is your problem? Is the file not being created or it is not being updated? Commented May 26, 2020 at 15:09

1 Answer 1

1

Your fields in your LibraryDatabase object are static. static things exist separate from the class (consider them at the same level as classes themselves. They have to be in a class, but only because in java classes are also namespaces). Your LibraryDatabase object therefore has no actual fields as far as serialization is concerned.

Make em non-static, will fix your problem. There's plenty else wrong with this code, but that is why you're seeing what you're seeing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a Lot. It Works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.