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