Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
d4d47fdaca
|
|||
b815d8945b
|
|||
c0f46727c6
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -23,4 +23,6 @@
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
replay_pid*
|
||||
.idea/
|
||||
.vscode/
|
||||
|
||||
|
@@ -6,7 +6,7 @@ public class App {
|
||||
Manager manager = new Manager();
|
||||
do {
|
||||
System.out.println("=======================");
|
||||
System.out.println("Agenda de contactos XML");
|
||||
System.out.println("Agenda de contactos TXT");
|
||||
System.out.println("=======================");
|
||||
System.out.println("1. Nuevo contacto");
|
||||
System.out.println("2. Buscar por nombre");
|
||||
|
@@ -2,10 +2,9 @@ package agenda;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import javax.xml.bind.*;
|
||||
import javax.xml.bind.annotation.*;
|
||||
|
||||
public class Manager {
|
||||
String fichero = "contactos.txt";
|
||||
String carpeta = "datos";
|
||||
ArrayList<Persona> personas;
|
||||
int limite;
|
||||
@@ -13,37 +12,59 @@ public class Manager {
|
||||
public Manager() {
|
||||
personas = new ArrayList<Persona>();
|
||||
limite = 5;
|
||||
JAXBContext contexto = JAXBContext.newInstance(Persona.class);
|
||||
cargar();
|
||||
}
|
||||
|
||||
public void cargar() {
|
||||
Unmarshaller um = contexto.createUnmarshaller();
|
||||
int num = 0;
|
||||
for (int i = 0; i < limite; i++) {
|
||||
String archivo = "contacto_" + i + ".xml";
|
||||
try {
|
||||
Persona persona = (Persona) um.unmarshal(new File(carpeta, archivo));
|
||||
personas.add(persona);
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
in = new BufferedReader(new FileReader(new File(carpeta, fichero)));
|
||||
} catch (Exception e) {
|
||||
// Si no existe el fichero no carga nada
|
||||
System.out.println("La agenda está vacía");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int num = 0;
|
||||
String linea = in.readLine();
|
||||
while (linea != null) {
|
||||
String[] datos = linea.split(",");
|
||||
personas.add(new Persona(datos[0], datos[1]));
|
||||
num++;
|
||||
linea = in.readLine();
|
||||
}
|
||||
System.out.println("Se han cargado " + num + " contactos");
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error cargando el archivo " + e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
// Si no existe el fichero no carga nada
|
||||
System.out.println("Error al cerrar el archivo " + e.getMessage());
|
||||
}
|
||||
}
|
||||
System.out.println("Se han cargado " + num + " contactos");
|
||||
}
|
||||
|
||||
public void guardar() {
|
||||
System.out.println("Guarda datos");
|
||||
Marshaller m = contexto.createMarshaller();
|
||||
BufferedWriter out = null;
|
||||
// Guarda siempre ordenado
|
||||
Collections.sort(personas);
|
||||
for (int i = 0; i < personas.size(); i++) {
|
||||
String archivo = "contacto_" + i + ".xml";
|
||||
try {
|
||||
m.marshal(personas.get(i), new File(carpeta, archivo));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error guardando el archivo " + e.getMessage());
|
||||
try {
|
||||
out = new BufferedWriter(new FileWriter(new File(carpeta, fichero)));
|
||||
for (Persona persona : personas) {
|
||||
out.write(persona.getNombre() + "," + persona.getTelefono());
|
||||
out.newLine();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error guardando el archivo " + e.getMessage());
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error al cerrar el archivo " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,5 @@
|
||||
package agenda;
|
||||
|
||||
import java.xml.bind.annotation.*;
|
||||
|
||||
@XmlRootElement(name = "persona")
|
||||
@XmlType(propOrder = { "nombre", "telefono" })
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class Persona implements Comparable {
|
||||
private String nombre;
|
||||
private String telefono;
|
||||
|
Reference in New Issue
Block a user