Actividad 1
This commit is contained in:
Binary file not shown.
@@ -1,59 +1,20 @@
|
||||
package org.palomafp.aadd.aez;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Fichero_01 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Obtiene dinámicamente la ruta C:\Users\aleja
|
||||
String usuarioHome = System.getProperty("user.home");
|
||||
|
||||
File fichero = new File(usuarioHome + "\\aadd\\fic01\\informacion_paises.txt");
|
||||
|
||||
// Comprobación previa de la existencia del archivo
|
||||
if (!fichero.exists()) {
|
||||
System.err.println("Error: No se encontró el fichero en " + fichero.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
// Lectura del fichero mediante BufferedReader y FileReader (Try-with-resources)
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||
public static void main(String[] args) throws Exception {
|
||||
String ruta = System.getProperty("user.home") + "\\aadd\\fic01\\informacion_paises.txt";
|
||||
BufferedReader br = new BufferedReader(new FileReader(ruta));
|
||||
String linea;
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
linea = linea.trim();
|
||||
|
||||
// Omitir líneas vacías o líneas de comentarios
|
||||
if (linea.isEmpty() || linea.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Separar los campos por comas
|
||||
String[] campos = linea.split(",");
|
||||
|
||||
if (campos.length == 4) {
|
||||
// Limpieza de comillas dobles y espacios en blanco
|
||||
String codContinente = campos[0].trim().replace("\"", "");
|
||||
int idPais = Integer.parseInt(campos[1].trim());
|
||||
String nombrePais = campos[2].trim().replace("\"", "");
|
||||
String capital = campos[3].trim().replace("\"", "");
|
||||
|
||||
// Generar la sentencia de inserción SQL
|
||||
String insertSQL = String.format("INSERT INTO T_PAIS VALUES ('%s', %d, '%s', '%s');", codContinente,
|
||||
idPais, nombrePais, capital);
|
||||
|
||||
// Imprimir por consola
|
||||
System.out.println(insertSQL);
|
||||
// Si la línea empieza por comilla, es un dato válido (ignora comentarios y vacías)
|
||||
if (linea.startsWith("\"")) {
|
||||
System.out.println("INSERT INTO T_PAIS VALUES (" + linea.replace('"', '\'') + ");");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error en la lectura del archivo: " + e.getMessage());
|
||||
} catch (NumberFormatException e) {
|
||||
System.err.println("Error al procesar formato numérico: " + e.getMessage());
|
||||
}
|
||||
br.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user