Primer commit ficheros08
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
package org.palomafp.aadd.ss.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.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -17,17 +13,24 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
|
||||
private String ruta = GestorConfiguracion.getRutaFicheroContinente();
|
||||
private String separador = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||
|
||||
private Path rutaNio = Path.of(ruta);
|
||||
@Override
|
||||
public List<Continente> obtenerTodos() {
|
||||
|
||||
List<Continente> listaContinentes = new ArrayList<>();
|
||||
try(BufferedReader br = new BufferedReader(new FileReader(ruta))){
|
||||
if (br.ready()) {
|
||||
br.readLine();
|
||||
}
|
||||
while(br.ready()) {
|
||||
String linea = br.readLine();
|
||||
try{
|
||||
// 1. Usamos Path y Files en lugar de BufferedReader
|
||||
//Path rutaNio = Path.of(ruta);
|
||||
List<String> todasLasLineas = Files.readAllLines(rutaNio);
|
||||
|
||||
boolean primerLinea = true;
|
||||
for(String linea : todasLasLineas) {
|
||||
// Nos saltamos la primera línea porque es la cabecera
|
||||
if(primerLinea) {
|
||||
primerLinea = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
String [] datos = linea.split(separador);
|
||||
listaContinentes.add(new Continente(datos[1].trim(), datos[2].trim()));
|
||||
}
|
||||
@@ -56,14 +59,16 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
System.out.println("El continente con codigo " + continente.getCodigo() + " ya existe.");
|
||||
return false;
|
||||
}
|
||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter(ruta, true))){
|
||||
//Salta de linea
|
||||
bw.newLine();
|
||||
bw.write("2026" + separador + continente.getCodigo() + separador + continente.getNombre());
|
||||
|
||||
try{
|
||||
// 2. Usamos Path y Files con StandardOpenOption.APPEND para añadir al final
|
||||
//Path rutaNio = Path.of(ruta);
|
||||
String lineaNueva = "2026" + separador + continente.getCodigo() + separador + continente.getNombre();
|
||||
// Files.write añade la línea directamente (el List.of crea una lista de 1 solo elemento)
|
||||
Files.write(rutaNio, List.of(lineaNueva), StandardOpenOption.APPEND);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al insertat el continente. " + e.getMessage());
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -114,11 +119,18 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
|
||||
|
||||
private void reescribirFichero(List<Continente> lista) {
|
||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter(ruta, false))){
|
||||
try{
|
||||
// 3. Usamos Files.write para sobreescribir el archivo entero
|
||||
//Path rutaNio = Path.of(ruta);
|
||||
List<String> lineasAEscribir = new ArrayList<>();
|
||||
//añadido la cabecera que tenías originalmente para no perderla al reescribir
|
||||
lineasAEscribir.add("anyo" + separador + "codigo_continente" + separador + "nombre_continente");
|
||||
for(Continente c : lista) {
|
||||
bw.write("2026" + separador + c.getCodigo() + separador + c.getNombre());
|
||||
bw.newLine();
|
||||
}
|
||||
lineasAEscribir.add("2026" + separador + c.getCodigo() + separador + c.getNombre());
|
||||
}
|
||||
// Si no le ponemos APPEND, Files.write borra el archivo y lo escribe entero de nuevo (¡Justo lo que queremos!)
|
||||
Files.write(rutaNio, lineasAEscribir);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al actualizar el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user