package com.company;
import org.json.JSONException;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private String filename = "users.txt";
private ArrayList people = new ArrayList();
private void run() {
System.out.println();
System.out.println("1: Добавить ");
System.out.println("2: Посмотреть ");
System.out.println("0: Выход ");
Scanner sc = new Scanner(System.in);
int c;
do {
c = sc.nextInt();
switch (c) {
case 1:
PersoneAdd();
break;
case 2:
PersoneView();
break;
case 0:
System.out.println("Выход");
break;
}
} while (c > 0);
}
void PersoneView() {
ArrayList<Person> newPeople = new ArrayList<Person>();
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
newPeople = (ArrayList<Person>) ois.readObject();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
for (Person p : newPeople)
System.out.printf("Имя:%s \r Регион: %s \n ", p.name, p.address);
}
private void PersoneAdd() {
String namee, adress;
Scanner sc = new Scanner(System.in);
System.out.print(" имя : ");
namee = sc.next();
System.out.print(" Регион: ");
adress = sc.next();
people.add(new Person(namee,adress));
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeObject(people);
System.out.println("Запись произведена");
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
class Person implements Serializable {
String name;
String address;
Person(String n, String aa) {
name = n;
address = aa;
}
}
public static void main(String[] args) throws JSONException, IOException, ClassNotFoundException {
Main menu = new Main();
menu.run();
}
}
writing aborted; java.io.NotSerializableException: com.company.Main
Как исправить ?!