Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Custom object serialization and deserialization to and from a file

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