In the following method:
generateFile (String filename, String country)
Generate a text file that contains the list of offices in the last country as the second parameter. The file must have the last name as the first parameter. The field separator must be the semicolon (;), the record separator the line break. All fields in the Offices table should be saved.
It correctly generates the file nombreArchivo.txt
as we see below in the main, only that it does not write the list of the country offices that I indicated in the second parameter, in this case, for example, " USES". Printing on the screen does it correctly (my capture); but I want you to generate the same listing in .txt
and do not do it. What could be the reason?
Method of the class that shows it on the screen well:
public static void generarArchivo(String nombreArchivo, String pais) throws SQLException, ClassNotFoundException, FileNotFoundException {
ConectarSingleton conexion=new ConectarSingleton();
Connection con = conexion.getConexion();
ResultSet rs = GestionClassic.consultar("select * from oficinas where pais ='"+pais+"'");
while(rs.next()) {
System.out.println(rs.getString(1)+";"+rs.getString(2)+";"+rs.getString(3)+";"+rs.getString(4)+";"
+rs.getString(5)+";"+rs.getString(6)+";"+rs.getString(7)+";"+rs.getString(8)+";"+rs.getString(9));
}
}
Method of the class that does not write data to the file:
public static void generarArchivo(String nombreArchivo, String pais) throws SQLException, ClassNotFoundException, FileNotFoundException {
ConectarSingleton conexion=new ConectarSingleton();
Connection con = conexion.getConexion();
PrintWriter pw = new PrintWriter(nombreArchivo);
ResultSet rs = GestionClassic.consultar("select * from oficinas where pais ='"+pais+"'");
while(rs.next()) {
pw.println(rs.getString(1)+";"+rs.getString(2)+";"+rs.getString(3)+";"+rs.getString(4)+";"
+rs.getString(5)+";"+rs.getString(6)+";"+rs.getString(7)+";"+rs.getString(8)+";"+rs.getString(9));
}
}
main:
public static void main(String[] args) {
try {
GestionClassic.generarArchivo("nombreArchivo.txt", "USA");
} catch (SQLException | ClassNotFoundException | FileNotFoundException ex) {
System.out.println(ex.getMessage());
}
}