import java.io.FileNotFoundException;Laufzeit:
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("Hallo Welt");
al.add ("Hallo Welt");
try {
FileOutputStream fileOut = new FileOutputStream("test");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(al);
out.close();
fileOut.close();
System.out.println("\nSerialisierung erfolgreich abgeschlossen...\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Serialisierung erfolgreich abgeschlossen...Der Inhalt der Binärdatei, die nach dem Öffnen erstellt wurde, sieht folgendermaßen aus:
import java.io.FileInputStream;Laufzeit:
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 Automatisch generierter catch-Block
e.printStackTrace();
}
System.out.println("Daten lesen: \n");
for(String o:arraylist)
System.out.println(o);
}
}
Lesedaten:Das war's! Zögern Sie nicht, Ihre Fragen zu stellen;)
hello world
hello world
Please disable your ad blocker and refresh the window to use this website.