Entrega Actividad 1.2: Tratamiento fichero txt con orientación a objetos
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,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
module Fichero_01 {
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.palomafp.aadd.ljl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.palomafp.aadd.ljl.vo.Continente;
|
||||
import org.palomafp.aadd.ljl.vo.Pais;
|
||||
|
||||
public class AppPrincipal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
imprimirInformacionPaises();
|
||||
|
||||
}
|
||||
|
||||
static public void imprimirInformacionPaises(){
|
||||
|
||||
// Generamos la ruta donde encontrar el fichero
|
||||
String rutaFichero = "C:\\Users\\lauuj\\aadd\\fic02\\informacion_paises.txt";
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(rutaFichero))) {
|
||||
|
||||
String linea;
|
||||
|
||||
// Mientras el fichero que leemos siga teniendo una siguiente linea que no sea null
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
// Descartamos las líneas que empiezan por '#' o '--' y las líneas en blanco
|
||||
if (!linea.startsWith("#") && !linea.startsWith("--") && !linea.trim().isEmpty() ) {
|
||||
|
||||
// Creamos un array con los datos del fichero teniendo en cuenta que el delimitador en ese fichero es la coma
|
||||
String[] datos = linea.split(",");
|
||||
|
||||
/*
|
||||
Limpiamos los espacios en blanco -> trim() y quitamos las comillas dobles -> replace("\"", ""),
|
||||
para quitar las comillas escapamos la " con el \
|
||||
El idPais no lleva comillas en el txt, así que solo le hacemos trim() y no un replace()
|
||||
*/
|
||||
String codContinente = datos[0].trim().replace("\"", "");
|
||||
|
||||
String idPais = datos[1].trim();
|
||||
// Casteamos los String a int para poder introducirlos en los objetos como atributos
|
||||
int idPaiscasteado = Integer.parseInt(idPais);
|
||||
|
||||
String nombrePais = datos[2].trim().replace("\"", "");
|
||||
String capital = datos[3].trim().replace("\"", "");
|
||||
|
||||
// Creamos los objetos
|
||||
Continente continente = new Continente(codContinente);
|
||||
Pais pais = new Pais(idPaiscasteado, nombrePais, capital, continente);
|
||||
|
||||
|
||||
// Imprimimos el objeto Pais y la sentencia SQL directamente por consola con un println
|
||||
System.out.println(pais);
|
||||
System.out.println("INSERT INTO T_PAIS (cod_continente, identificador, capital, nombre_pais) "
|
||||
+ "VALUES ('" + codContinente + "', " + idPaiscasteado + ", '" + capital+ "', '" + nombrePais + "');");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al leer el fichero. Comprueba la ruta: " + e.getMessage());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.palomafp.aadd.ljl.vo;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
// Constructores
|
||||
public Continente() {
|
||||
|
||||
}
|
||||
public Continente(String codigo) {
|
||||
super();
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
|
||||
// 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 + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.palomafp.aadd.ljl.vo;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String capital;
|
||||
private Continente continente;
|
||||
|
||||
// Constructores
|
||||
public Pais() {
|
||||
|
||||
}
|
||||
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||
super();
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
// Getters y Setters
|
||||
public int getIdentificador() {
|
||||
return identificador;
|
||||
}
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
public String getCapital() {
|
||||
return capital;
|
||||
}
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void setIdentificador(int identificador) {
|
||||
this.identificador = identificador;
|
||||
}
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
// Método toString
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital + ", continente="
|
||||
+ continente + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user