Primer commit ficheros08
This commit is contained in:
@@ -1,12 +1,8 @@
|
|||||||
package org.palomafp.aadd.ss.dao.impl;
|
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.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -17,17 +13,24 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
|||||||
|
|
||||||
private String ruta = GestorConfiguracion.getRutaFicheroContinente();
|
private String ruta = GestorConfiguracion.getRutaFicheroContinente();
|
||||||
private String separador = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
private String separador = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||||
|
private Path rutaNio = Path.of(ruta);
|
||||||
@Override
|
@Override
|
||||||
public List<Continente> obtenerTodos() {
|
public List<Continente> obtenerTodos() {
|
||||||
|
|
||||||
List<Continente> listaContinentes = new ArrayList<>();
|
List<Continente> listaContinentes = new ArrayList<>();
|
||||||
try(BufferedReader br = new BufferedReader(new FileReader(ruta))){
|
try{
|
||||||
if (br.ready()) {
|
// 1. Usamos Path y Files en lugar de BufferedReader
|
||||||
br.readLine();
|
//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;
|
||||||
}
|
}
|
||||||
while(br.ready()) {
|
|
||||||
String linea = br.readLine();
|
|
||||||
String [] datos = linea.split(separador);
|
String [] datos = linea.split(separador);
|
||||||
listaContinentes.add(new Continente(datos[1].trim(), datos[2].trim()));
|
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.");
|
System.out.println("El continente con codigo " + continente.getCodigo() + " ya existe.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter(ruta, true))){
|
try{
|
||||||
//Salta de linea
|
// 2. Usamos Path y Files con StandardOpenOption.APPEND para añadir al final
|
||||||
bw.newLine();
|
//Path rutaNio = Path.of(ruta);
|
||||||
bw.write("2026" + separador + continente.getCodigo() + separador + continente.getNombre());
|
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) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error al insertat el continente. " + e.getMessage());
|
System.err.println("Error al insertat el continente. " + e.getMessage());
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -114,11 +119,18 @@ public class ContinenteDaoCSV implements IContinenteDAO {
|
|||||||
|
|
||||||
|
|
||||||
private void reescribirFichero(List<Continente> lista) {
|
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) {
|
for(Continente c : lista) {
|
||||||
bw.write("2026" + separador + c.getCodigo() + separador + c.getNombre());
|
lineasAEscribir.add("2026" + separador + c.getCodigo() + separador + c.getNombre());
|
||||||
bw.newLine();
|
|
||||||
}
|
}
|
||||||
|
// 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) {
|
} catch (IOException e) {
|
||||||
System.out.println("Error al actualizar el fichero: " + e.getMessage());
|
System.out.println("Error al actualizar el fichero: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user