Hi so my code is this
public static void main(String[] args) throws IOException, ClassNotFoundException {
char opc;
do {
System.out.println("1- Agregar contactos\n2- Modificar contacto\n3- Mostrar\n4- Salir");
opc = in.nextLine().charAt(0);
switch (opc) {
case '1':
agregarContacto();
break;
case '2':
modificarContacto();
break;
case '3':
mostrarContactos();
}
} while (opc != '4');
}
public static void agregarContacto() throws IOException {
FileOutputStream fos = new FileOutputStream(fichero, true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < 4; i++) {
System.out.println("Introduce " + textoParaMostrar[i]);
textoAGuardar[i] = in.nextLine();
}
Persona nuevoContacto = new Persona(textoAGuardar[0], textoAGuardar[1], textoAGuardar[2], textoAGuardar[3]);
oos.writeObject(nuevoContacto);
oos.flush();
}
public static void mostrarContactos() throws IOException, ClassNotFoundException {
if (fichero.exists()) {
FileInputStream fis = new FileInputStream(fichero);
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
try {
Persona p = (Persona) ois.readObject();
System.out.println(p.toString());
} catch (EOFException e) {
break;
}
}
ois.close(); // Cierra el ObjectInputStream después de leer todos los contactos
} else {
System.out.println("El archivo no existe o no hay contactos almacenados.");
}
}