Forum Stats

  • 3,838,514 Users
  • 2,262,378 Discussions
  • 7,900,678 Comments

Discussions

Custom object serialization and deserialization to and from a file

0ae0106a-7b4e-4861-88d2-79b7d9dd850f
edited Mar 17, 2020 7:08PM in Java Programming

Hi everyone...

So I have a class called: CallsLogObject see hereunder:

package com.example.test;import java.io.Serializable;public class CallsLogObject implements Serializable {    private String contactName;    private String phoneNumber;    private String callType;    private String callDateTime;    private String callDuration;    private String simIdNumber = "null";    private String simNumber = "null";    private String simOperator = "null";    private String imei = "null";    private String simCountryISO = "null";    public CallsLogObject(){            super();    }    public void setContactName(String contactName) {        this.contactName = contactName;    }    public void setPhoneNumber(String phoneNumber) {        this.phoneNumber = phoneNumber;    }    public void setCallType(String callType) {        this.callType = callType;    }    public void setCallDateTime(String callDateTime) {        this.callDateTime = callDateTime;    }    public void setCallDuration(String callDuration) {        this.callDuration = callDuration;    }    public void setSimIdNumber(String simIdNumber) { this.simIdNumber = simIdNumber; }    public void setSimNumber(String simNumber) {this.simNumber = simNumber;}    public void setSimOperator(String simOperator) { this.simOperator = simOperator;}    public void setImei(String imei) { this.imei = imei; }    public void setSimCountryIso(String simCountryIso) { this.simCountryISO = simCountryIso; }    public String getContactName() {        return contactName;    }    public String getPhoneNumber() {        return phoneNumber;    }    public String getCallType() {        return callType;    }    public String getCallDateTime() {        return callDateTime;    }    public String getCallDuration() {        return callDuration;    }    public String getSimIdNumber() {        return simIdNumber;    }    public String getSimNumber(){        return simNumber;    }    public String getSimOperator() { return simOperator; }    public String getImei() { return imei; }    public String getSimCountryISO() { return simCountryISO; }}
 public String getCallType() {        return callType;    }    public String getCallDateTime() {        return callDateTime;    }    public String getCallDuration() {        return callDuration;    }    public String getSimIdNumber() {        return simIdNumber;    }    public String getSimNumber(){        return simNumber;    }    public String getSimOperator() { return simOperator; }    public String getImei() { return imei; }    public String getSimCountryISO() { return simCountryISO; }}

I append CallsLogObjects to a file(one by one or more in a loop) so I use also hereunder class:

package com.example.test;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.OutputStream;/*Class needed for correct write objects which are appended to a file using ObjectOutputStream */public class AppendingObjectOutputStream extends ObjectOutputStream {public AppendingObjectOutputStream(OutputStream out) throws IOException {    super(out);}    @Override    protected void writeStreamHeader() throws IOException {        // do not write a header, but reset:        reset();    }}

This is a way I write CallsLogObjects to a file:

//....

else        {            try {                fos = new FileOutputStream(callsLogPathBinFile.getPathToBinFile(), true);                                AppendingObjectOutputStream appendingObjectOutputStream = new AppendingObjectOutputStream(fos);                                //get the last CallsLogObject from vect of CallsLogObjects                int lastItem = callsLogData.getCallsLogManager().getCallsLogObjectsVect().size() - 1;                //Append the last CallsLogObject to the CallsLogObject.bin file                 appendingObjectOutputStream.writeObject(callsLogData.getCallsLogManager().getCallsLogObjectsVect().get(lastItem));                fos.flush();                fos.close();            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }        }

Here above I showed in code how I write CallsLogObjects to CallsLogObject.bin file. The project is written in Android Studio.

Using JavaFx I created a new project and I implemented in this project CallsLogObject class (I created the same package

the CallsLogObject class was in Android Studio: com.example.test so in JavaFx the CallsLogObject class is in the same package com.example.test)

Going further I tryed to read CallsLogObjects from CallsLogObject.bin file and print it in javaFx poject console see

hereunder piece of the code:

@Override    public void readBinaryFile(File file) throws IOException, ClassNotFoundException {        Object callsLogObject;        FileInputStream fileInputStream = new FileInputStream(file);        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);        //couts objects in CallsLogObject.bin file        int counterObject = 0;        try {            while (true) {                   callsLogObject = objectInputStream.readObject();                if (callsLogObject instanceof CallsLogObject) {                                        CallsLogObject callsLogObject1 = (CallsLogObject) objectInputStream.readObject();                    //print in console only callDateTime member of CallsLogObject                    System.out.println(callsLogObject1.getCallDateTime());                                        counterObject = counterObject + 1;                }            }        } catch (EOFException e) {            System.out.println("End of file has beeen reached..." + String.valueOf(counterObject));        }        objectInputStream.close();        fileInputStream.close();    }

   

Everything went ok (I mean readin the objects from file) but one thing: All CallsLogObjects are read except the last one.

Hereunder snipet of consol showed what is printed:

poniedziałek 13:43 16.marca.2020

poniedziałek 14:58 16.marca.2020

poniedziałek 17:18 16.marca.2020

End of file has beeen reached...307

"poniedziałek 17:18 16.marca.2020" - this is callDateTime member variable of from one before last CallsLogObject the last

callDateTime member variable should be poniedziałek 17:51 16.marca.2020 see the scrap of the CallsLogObject.bin file:

The one before last object see hereunder:

com.example.test.CallsLogObject  }śmM|´

L  callDateTimet  Ljava/lang/String;L  callDurationq ~  L  callTypeq ~  L  contactNameq ~  L  imeiq ~  L  phoneNumberq ~  L

simCountryISOq ~  L  simIdNumberq ~  L     simNumberq ~  L  simOperatorq ~  xpt !poniedziaĹ‚ek 17:18 16.marca.2020t  01:41t  OUTGOINGppt     713068888pt  8948031552691115807t  nullpysr

The last object in the file which I can not read from file, see hereunder:

com.example.test.CallsLogObject  }śmM|´

L  callDateTimet  Ljava/lang/String;L  callDurationq ~  L  callTypeq ~  L  contactNameq ~  L  imeiq ~  L  phoneNumberq ~  L

simCountryISOq ~  L  simIdNumberq ~  L     simNumberq ~  L  simOperatorq ~  xpt !poniedziaĹ‚ek 17:51 16.marca.2020t  01:41t  OUTGOINGppt     713068888pt  8948031552691115807t  nullp

So the question is why on earth the last CallsLogObject from the CallsLogObject.bin file can not be read????

Wiadomość była edytowana przez: 0ae0106a-7b4e-4861-88d2-79b7d9dd850f