Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it recommended to serialize & deserialize objects that are stored in arrayList?

In my small bank application, users have to input some value (name, SSN, amount etc..) and they get stored in an arrayList. The arrayList size is dynamic.

But problem with this one is I loose all data once I terminate the application. That leads me to think about the implementation of writing and reading file (file I/O).

Now I also have come to know about something called serialization and deserialization, though I am not quite sure in what situation this need to be implemented.

Do I need it in my particular case or simply writing into and reading from file will be enough?

What serialization and deserialization has to do with file I/O?

[NOTE: I will give more info if necessary]

like image 753
Munira Avatar asked Apr 24 '15 09:04

Munira


People also ask

Why is serialization not good?

It is not future-proof for small changes If you mark your classes as [Serializable] , then all the private data not marked as [NonSerialized] will get dumped. You have no control over the format of this data. If you change the name of a private variable, then your code will break.

What happens if we don't serialize?

What happens if you try to send non-serialized Object over network? When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

Is it necessary to implement serializable?

If a super class doesn't implement Serializable, then when a subclass object is deserialized, the super class constructor will run. Static variables are not serialized because they are not part of the object itself. If you serialize a collection or an array, every element must be serializable.

What are the disadvantages of serialization?

Since serialization does not offer any transaction control mechanisms per se, it is not suitable for use within applications needing concurrent access without making use of additional APIs.


4 Answers

This is where a Database comes into picture. To start with, you can use MySQL DB - it' an excellent FREE Database for small to medium size business apps. Later, if you intend to deploy your app to production - with large number of users & advance features, and are ready to pay a price for it - you might consider other databases like Oracle etc.

Storing info to files ((De)Serialization) is not recommended for any practical application.

like image 72
Raúl Avatar answered Oct 26 '22 22:10

Raúl


Serialization is a mechanism where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

ArrayList already implements Serializable, so in your example you could write something like this:

 ArrayList<String> al=new ArrayList<String>();
 al.add("Jean");
 al.add("Pierre");
 al.add("John");

 try{
  FileOutputStream fos= new FileOutputStream("myfile.txt");
  ObjectOutputStream oos= new ObjectOutputStream(fos);
  oos.writeObject(al);
  oos.close();
  fos.close();
 }catch(IOException ioe){
  ioe.printStackTrace();
 }

Here we save the list al in the file myfile.txt.

To read the file and get your ArrayList back, you would use ObjectInputStream:

FileInputStream fis = new FileInputStream("myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);

ArrayList<String> list = (ArrayList<String>) ois.readObject();

ois.close();
like image 25
couscousman Avatar answered Oct 26 '22 22:10

couscousman


Serialization is required when you want to write instances of your own class to a file. In your case, you can create a java class to hold all the values about customer, then override hashCode() and equals(), and then write your object to file. http://www.tutorialspoint.com/java/java_serialization.htm

Also, if you want, you can store individual field in file as well as int or String.

Though I would suggest to use a database to store all this information. But it seems you are a student and still in learning phase. So, interacting with DB right away might not be a good approach as of now.

like image 29
Aakash Avatar answered Oct 26 '22 23:10

Aakash


Yes, you can use arraylist for serialization and deserialization. 
Whenever u want to write and read the object into file and from file
respectively then u need to be object should be serialized and object 
write into the file in byte stream format.that means ur data will be secure in 
stream.you can used serialization interface:-
To persist data for future use.
To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
To "flatten" an object into array of bytes in memory.
To exchange data between applets and servlets.
To store user session in Web applications.
To activate/passivate enterprise java beans.
To send objects between the servers in a cluster.
and more............
like image 44
Mohammad Sadik Avatar answered Oct 26 '22 23:10

Mohammad Sadik