Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package file.example;
- import java.io.*;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class Main {
- private static String dir = "src/";
- private static String file = "file";
- public static void main(String[] args) {
- try (
- ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(dir.concat(file)));
- ObjectInputStream is = new ObjectInputStream(new FileInputStream(dir.concat(file)))) {
- List<Serializable> list = new ArrayList<>();
- list.add(new Individual("Deivid", 15));
- list.add(new Individual("Ivan", 18));
- list.add(new Individual("Petio", 15));
- int counter = 0;
- for (Serializable el : list) {
- os.writeObject(el);
- counter++;
- }
- list.clear();
- for (int i = 0; i < counter; i++) {
- Serializable inf = (Serializable) is.readObject();
- list.add(inf);
- }
- list.forEach(System.out::println);
- } catch (IOException |
- ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement