Entrega Actividad 1.7: Uso del fichero properties
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
# Ignorar la carpeta de compilados
|
||||||
|
bin/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Ignorar archivos de configuración de Eclipse
|
||||||
|
.settings/
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# config/application.properties
|
||||||
|
ruta.fichero.continente=C:\\Users\\lauuj\\aadd\\fic07\\informacion_continentes.csv
|
||||||
|
separador.csv.continente=#
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
module Ficheros_03 {
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
package org.palomafp.aadd.ljl;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public class GestorMapamundi {
|
||||||
|
|
||||||
|
private IContinenteDAO continenteDao = new ContinenteDaoFichero();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// lo que nos devuelve el array de los argumentos son Strings, asi que lo parseamos
|
||||||
|
int opcion = Integer.parseInt(args[0]);
|
||||||
|
|
||||||
|
GestorMapamundi gestor = new GestorMapamundi();
|
||||||
|
Continente continente;
|
||||||
|
|
||||||
|
switch(opcion) {
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
// Obtenemos una Lista de continentes
|
||||||
|
List<Continente> continentes = gestor.obtenerListaDeContinentes();
|
||||||
|
|
||||||
|
for (Continente cont : continentes) {
|
||||||
|
System.out.println(cont);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
// Obtenemos un Continente por su código
|
||||||
|
continente = gestor.obtenerContinenteDeCodigo(args[1]);
|
||||||
|
System.out.println("El continente que buscas es: " + continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
// Insertaremos un continente
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
gestor.insertarContinente(continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
// Eliminaremos un continente
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
gestor.borrarContinente(continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
// Actualizaremos un continente
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
gestor.actualizarContinente(continente);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Opción no esperada");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void actualizarContinente(Continente continente) {
|
||||||
|
|
||||||
|
Continente continenteFichero = continenteDao.obtenerContinentePorCod(continente.getCodigo());
|
||||||
|
if (continenteFichero != null) {
|
||||||
|
continenteDao.actualizarContinente(continente);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void borrarContinente(Continente continente) {
|
||||||
|
Continente continenteFichero = continenteDao.obtenerContinentePorCod(continente.getCodigo());
|
||||||
|
if (continenteFichero != null) {
|
||||||
|
continenteDao.eliminarContinente(continente);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertarContinente(Continente continente) {
|
||||||
|
|
||||||
|
Continente continenteFichero = continenteDao.obtenerContinentePorCod(continente.getCodigo());
|
||||||
|
if (continenteFichero == null) {
|
||||||
|
continenteDao.insertarContinenteNuevo(continente);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente obtenerContinenteDeCodigo(String codigoContinente) {
|
||||||
|
Continente continente = continenteDao.obtenerContinentePorCod(codigoContinente);
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Continente> obtenerListaDeContinentes() {
|
||||||
|
List<Continente> listaContinentes = continenteDao.obtenerListaContinentes();
|
||||||
|
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package org.palomafp.aadd.ljl.config;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class ConfigManager {
|
||||||
|
|
||||||
|
private Properties properties;
|
||||||
|
|
||||||
|
public ConfigManager() {
|
||||||
|
|
||||||
|
properties = new Properties();
|
||||||
|
|
||||||
|
// Leemos el archivo application.properties dentro de la carpeta config
|
||||||
|
try (FileInputStream fis = new FileInputStream("config/application.properties")) {
|
||||||
|
|
||||||
|
// el .load() se encarga de leer el archivo físico y "memorizar" todo su contenido, para ello necesita la tuberia FileInputStream
|
||||||
|
properties.load(fis);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al cargar config/application.properties: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRutaFicheroContinente() {
|
||||||
|
|
||||||
|
/*
|
||||||
|
Una vez que ya has hecho el .load() y el objeto Properties tiene todos los datos cargados,
|
||||||
|
este método .getProperty() -> sirve para extraer la información.
|
||||||
|
Tú le das la "clave" y él te devuelve el "valor", que en este caso sería la ruta del fichero.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Si no encuentra la clave, devuelve la ruta por defecto para que el programa no falle (segundo valor por si acaso falla)
|
||||||
|
return properties.getProperty("ruta.fichero.continente", "C:\\Users\\lauuj\\aadd\\fic07\\informacion_continentes.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSeparadorCsv() {
|
||||||
|
// Si no encuentra la clave, devuelve "#" por defecto
|
||||||
|
return properties.getProperty("separador.csv.continente", "#");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
public List<Continente> obtenerListaContinentes();
|
||||||
|
|
||||||
|
public Continente obtenerContinentePorCod(String codContinente);
|
||||||
|
|
||||||
|
public boolean insertarContinenteNuevo(Continente continente);
|
||||||
|
|
||||||
|
public boolean eliminarContinente(Continente continente);
|
||||||
|
|
||||||
|
public boolean actualizarContinente(Continente continente);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,242 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.config.ConfigManager;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
private String ruta;
|
||||||
|
private String separador;
|
||||||
|
|
||||||
|
// Constructor para cargar las propiedades al instanciar el DAO
|
||||||
|
public ContinenteDaoFichero() {
|
||||||
|
|
||||||
|
// Instanciamos la clase gestora
|
||||||
|
ConfigManager config = new ConfigManager();
|
||||||
|
|
||||||
|
// Le pedimos a la clase gestora los valores y los guardamos
|
||||||
|
this.ruta = config.getRutaFicheroContinente();
|
||||||
|
this.separador = config.getSeparadorCsv();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerListaContinentes() {
|
||||||
|
|
||||||
|
List<Continente> listaContinentes = new ArrayList<Continente>();
|
||||||
|
File fichero = new File(ruta);
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(fichero))){
|
||||||
|
|
||||||
|
// Nos saltamos la primera línea que contiene los nombres de los campos.
|
||||||
|
br.readLine();
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
while ((linea=br.readLine()) != null) {
|
||||||
|
|
||||||
|
String[] partes = linea.split(separador);
|
||||||
|
String codContinente = partes[1].trim();
|
||||||
|
String nombreContinente = partes[2].trim();
|
||||||
|
|
||||||
|
// Se añade un nuevo continente a la lista de continentes obtenidos.
|
||||||
|
listaContinentes.add(obtenerObjetoContinente(codContinente, nombreContinente));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCod(String codContinente) {
|
||||||
|
|
||||||
|
Continente continente = null;
|
||||||
|
File fichero = new File(ruta);
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||||
|
|
||||||
|
br.readLine();
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
while((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
String[] partes = linea.split(separador);
|
||||||
|
String codigoFichero = partes[1].trim();
|
||||||
|
|
||||||
|
if (codigoFichero.equals(codContinente)) {
|
||||||
|
|
||||||
|
continente = obtenerObjetoContinente(codigoFichero, partes[2].trim());
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.getMessage();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insertarContinenteNuevo(Continente continente) {
|
||||||
|
|
||||||
|
// comprobación de que el codigo del continente no existe esta en la app principal porque es la lógica
|
||||||
|
// Si llamamos a este método es porque ya sabemos que no existe el continente asi que lo creamos
|
||||||
|
File fichero = new File(ruta);
|
||||||
|
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fichero, true))) {
|
||||||
|
|
||||||
|
// hacemos el salto de linea
|
||||||
|
bw.newLine();
|
||||||
|
|
||||||
|
bw.write( "2026" + separador + continente.getCodigo() + separador + continente.getNombre());
|
||||||
|
|
||||||
|
System.out.println("Linea insertada correctamente");
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean eliminarContinente(Continente continente) {
|
||||||
|
|
||||||
|
File fichero = new File(ruta);
|
||||||
|
List<String> lineasAConservar = new ArrayList<>();
|
||||||
|
|
||||||
|
// 1. Leemos el archivo y guardamos todo EXCEPTO el continente a borrar
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
String lineaTrim = linea.trim();
|
||||||
|
|
||||||
|
// Si la línea está vacía o es una cabecera, la conservamos tal cual
|
||||||
|
if (lineaTrim.isEmpty() || !Character.isDigit(lineaTrim.charAt(0))) {
|
||||||
|
lineasAConservar.add(linea);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] partes = lineaTrim.split(separador);
|
||||||
|
String codContinente = partes[1].trim();
|
||||||
|
|
||||||
|
// Si el código NO coincide, guardamos la línea. Si coincide, la ignoramos (se elimina)
|
||||||
|
if (!codContinente.equals(continente.getCodigo())) {
|
||||||
|
lineasAConservar.add(linea);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Sobrescribimos el fichero completo con las lineas de la lista
|
||||||
|
// FileWriter con parámetro 'false' (o sin él) sobrescribe el fichero desde 0 (se vacía)
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fichero, false))) {
|
||||||
|
|
||||||
|
for (int i = 0; i < lineasAConservar.size(); i++) {
|
||||||
|
|
||||||
|
// seleccionamos una a una las lineas de la lista que guarda las lineas no borradas
|
||||||
|
bw.write(lineasAConservar.get(i));
|
||||||
|
|
||||||
|
/* Como tenemos que hacer un salto de linea por cada linea que escribamos (para que no se apelotonen)
|
||||||
|
hay que tener en cuenta que luego no queremos dejar un espacio en blanco al final del csv
|
||||||
|
por lo que solo metemos el salto de linea hasta la penultima linea incluida (lineasAConservar.size() - 1)
|
||||||
|
*/
|
||||||
|
if (i < lineasAConservar.size() - 1) {
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("Continente eliminado correctamente.");
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean actualizarContinente(Continente continente) {
|
||||||
|
|
||||||
|
File fichero = new File(ruta);
|
||||||
|
List<String> lineasActualizadas = new ArrayList<>();
|
||||||
|
|
||||||
|
// 1. Leemos y modificamos solo la línea que coincida
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
String lineaTrim = linea.trim();
|
||||||
|
|
||||||
|
if (lineaTrim.isEmpty() || !Character.isDigit(lineaTrim.charAt(0))) {
|
||||||
|
lineasActualizadas.add(linea);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] partes = lineaTrim.split(separador);
|
||||||
|
String codContinente = partes[1].trim();
|
||||||
|
|
||||||
|
if (codContinente.equals(continente.getCodigo())) {
|
||||||
|
// Reconstruimos la línea: partes[0] (año) + partes[1](código) + nombre NUEVO
|
||||||
|
String nuevaLinea = partes[0] + separador + partes[1] + separador + continente.getNombre();
|
||||||
|
lineasActualizadas.add(nuevaLinea);
|
||||||
|
} else {
|
||||||
|
// Las demás líneas se quedan como estaban
|
||||||
|
lineasActualizadas.add(linea);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Sobrescribimos el fichero (que se vacía al empezar)
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fichero, false))) {
|
||||||
|
for (int i = 0; i < lineasActualizadas.size(); i++) {
|
||||||
|
|
||||||
|
// cogemos de la lista de lineas una
|
||||||
|
bw.write(lineasActualizadas.get(i));
|
||||||
|
|
||||||
|
// hacemos el salto de linea (salvo que sea la última línea)
|
||||||
|
if (i < lineasActualizadas.size() - 1) {
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("Continente actualizado correctamente.");
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente obtenerObjetoContinente(String codigoContinente, String nombreContiente) {
|
||||||
|
// Obtenemos el objeto Continente a partir del código.
|
||||||
|
Continente continente = new Continente();
|
||||||
|
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
continente.setNombre(nombreContiente);
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.palomafp.aadd.ljl.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
// Constructores
|
||||||
|
public Continente() {
|
||||||
|
|
||||||
|
}
|
||||||
|
public Continente(String codigo, String nombre) {
|
||||||
|
super();
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Getters y setters
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generamos el método toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user