Is it recommended to serialize and deserialize objects stored in an arrayList?

In my small banking application, users must enter some value (name, SSN, quantity, etc.) and they are stored in an array. The array size is dynamic.

But the problem with this is that I lose all data after the application terminates. It makes me think about the implementation of writing and reading a file (input / output).

Now I also learned about something called serialization and deserialization, although I'm not quite sure in which situation this needs to be implemented.

Do I need it in my particular case or is it enough to write and read from a file?

What serialization and deserialization are related to file inputs?

[NOTE: I will give more information if necessary]

+5
source share
4 answers

The Database is displayed here. For starters, you can use MySQL DB - this is an excellent FREE database for small and medium business applications. Later, if you intend to deploy your application for production - with a large number of users and advanced features, and are willing to pay a price for it - you can consider other databases, such as Oracle , etc.

Saving information in files ((De) Serialization) is not recommended for any practical use.

+2
source

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

ArrayList already implements Serializable, so in your example you can 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 return an ArrayList, you must use an ObjectInputStream:

 FileInputStream fis = new FileInputStream("myfile.txt"); ObjectInputStream ois = new ObjectInputStream(fis); ArrayList<String> list = (ArrayList<String>) ois.readObject(); ois.close(); 
+2
source

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

In addition, if you want, you can save a separate field in the file, as well as int or String .

Although I would suggest using a database to store all this information. But you seem to be a student and still studying. Thus, interacting with the database right away may not be a very good approach.

+1
source
 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............ 
+1
source

All Articles