Saving an ArrayList to a Java File
Java provides a mechanism where an object can be represented as a sequence of bits that contains the data of that object and this information: its type and the types of the data saved in the object.After the serialized object has been successfully saved to the file, it can be read from the file without problems and then de-serialize it. The bits that represent the object and its data can be used to recreate the object in memory.
The ObjectInputStream and ObjectOutputStream classes are two high-level data streams that contain the methods for saving and reading the contents of an ArrayList from a .
ArrayList is serializable by default. This means that you don't need to implement the Serializable interface in order to serialize an ArrayList.
Save an ArrayList to a
This class creates a test file that will have an ArrayList object as a bit stream. The test file is used to save and recreate the object from bitstreams. Note that we didn't implement the Serializable interface in this example because ArrayList is already serialized by default.import java.io.FileNotFoundException;Runtime:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class SerializeArrayList {
public static void main(String[] args) {
ArrayListal=new ArrayList ();
al.add("hello world");
al.add ("hello world");
try {
FileOutputStream fileOut = new FileOutputStream("test");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(al);
out.close();
fileOut.close();
System.out.println("\nSerialization completed successfully...\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Serialization completed successfully...The contents of the binary created after opening look like this:
Read and create an ArrayList from a
In this class, we retrieve the data stream in the form of bits from the test file that we stored using the class above. We convert the returned object to ArrayList with the cast and we display the elements of ArrayList. Looking at the output, we get the same items that we added to the list before serialization.import java.io.FileInputStream;Runtime:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class SerializeArrayList {
public static void main(String[] args) {
//read
ArrayList< String> arraylist= new ArrayList< String> ();
try {
FileInputStream fileIn = new FileInputStream("test");
ObjectInputStream ois = new ObjectInputStream(fileIn);
arraylist = (ArrayList) ois.readObject();
ois.close();
fileIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Read data: \n");
for(String o:arraylist)
System.out.println(o);
}
}
Read data:That's it! Do not hesitate to ask your questions;)
hello world
hello world