Actividad 1.8 Tratamiento de ficheros (java.nio) VI - CRUD + DAO + Properties (MapaMundi)
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,2 +1,3 @@
|
||||
continente.ruta.fichero=C:\\Users\\gonza\\aadd\\fic05\\informacion_continentes.csv
|
||||
continente.csv.separador=#
|
||||
continente.anyo=2026
|
||||
@@ -1,10 +1,11 @@
|
||||
package org.palomafp.aadd.gdo.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -14,9 +15,10 @@ import org.palomafp.aadd.gdo.model.Continente;
|
||||
|
||||
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
|
||||
private static final String RUTA_FICHERO = GestorConfiguracion.getRutaFicheroContinente();
|
||||
private static final Path RUTA_FICHERO = Paths.get(GestorConfiguracion.getRutaFicheroContinente());
|
||||
private static final String SEPARADOR = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||
private static final String ANYO = "2026";
|
||||
private static final Charset CHARSET = StandardCharsets.UTF_8;
|
||||
private static final String ANYO = GestorConfiguracion.getAnyoContinente();
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codContinente) {
|
||||
@@ -31,23 +33,16 @@ public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
@Override
|
||||
public List<Continente> obtenerContinentes() {
|
||||
List<Continente> continentes = new ArrayList<>();
|
||||
List<String> lineas = leerLineas();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||
br.readLine();
|
||||
|
||||
String linea;
|
||||
while ((linea = br.readLine()) != null) {
|
||||
String[] campos = linea.split(SEPARADOR);
|
||||
|
||||
String codigo = campos[1].trim();
|
||||
String nombre = campos[2].trim();
|
||||
|
||||
continentes.add(new Continente(codigo, nombre));
|
||||
// la línea 0 es la cabecera
|
||||
for (int i = 1; i < lineas.size(); i++) {
|
||||
if (lineas.get(i).isBlank()) {
|
||||
continue;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||
String[] campos = lineas.get(i).split(SEPARADOR);
|
||||
continentes.add(new Continente(campos[1].trim(), campos[2].trim()));
|
||||
}
|
||||
|
||||
return continentes;
|
||||
}
|
||||
|
||||
@@ -99,24 +94,17 @@ public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
}
|
||||
|
||||
private List<String> leerLineas() {
|
||||
List<String> lineas = new ArrayList<>();
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||
String linea;
|
||||
while ((linea = br.readLine()) != null) {
|
||||
lineas.add(linea);
|
||||
}
|
||||
try {
|
||||
return new ArrayList<>(Files.readAllLines(RUTA_FICHERO, CHARSET));
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return lineas;
|
||||
}
|
||||
|
||||
private boolean escribirLineas(List<String> lineas) {
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_FICHERO))) {
|
||||
for (String linea : lineas) {
|
||||
bw.write(linea);
|
||||
bw.newLine();
|
||||
}
|
||||
try {
|
||||
Files.write(RUTA_FICHERO, lineas, CHARSET);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al escribir el fichero: " + e.getMessage());
|
||||
|
||||
@@ -27,4 +27,8 @@ public class GestorConfiguracion {
|
||||
public static String getSeparadorCsvFicheroContinente() {
|
||||
return PROPIEDADES_CONFIGURACION.getProperty("continente.csv.separador");
|
||||
}
|
||||
|
||||
public static String getAnyoContinente() {
|
||||
return PROPIEDADES_CONFIGURACION.getProperty("continente.anyo");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user