import java.io.FileNotFoundException;Exécution:
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("bonjour le monde");
try {
FileOutputStream fileOut = new FileOutputStream("test");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(al);
out.close();
fileOut.close();
System.out.println("\nSerialisation terminée avec succès...\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Serialisation terminée avec succès...Le contenu du fichier binaire créé après l'ouverture ressemble à ceci:
import java.io.FileInputStream;Exécution:
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) {
//lecture
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("Lire les données: \n");
for(String o:arraylist)
System.out.println(o);
}
}
Lire les données:C'est tout! N'hésitez pas à poser vos questions ;)
hello world
bonjour le monde
Please disable your ad blocker and refresh the window to use this website.