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:
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
Please disable your ad blocker and refresh the window to use this website.