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 («привет, мир»);
try {
FileOutputStream fileOut = new FileOutputStream("test");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(al);
out.close();
fileOut.close();
System.out.println("\nСериализация успешно завершена...\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Сериализация успешно завершена...Содержимое двоичного файла, созданного после открытия, выглядит следующим образом:
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< Строка> arraylist= новый ArrayList< Строка> ();
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 Автоматически сгенерированный catch block
e.printStackTrace();
}
System.out.println("Чтение данных: \n");
for(String o:arraylist)
System.out.println(o);
}
}
Read data:Вот и все! Не стесняйтесь задавать свои вопросы ;)
hello world
hello world
Please disable your ad blocker and refresh the window to use this website.