Read and process more than one JAVA flat file

1

I have a method that allows me to read and load a flat file into the database, read it and separate it until finally loading it into the table. The problem is that I would like to be able to make a "massive load of data" that is, be able to read and load more than one file selected by the user. I currently display the .txt in a list. Help please !!!

public class CargaConciliacion {     public boolean leeArchivo (String doc) {         boolean process = true;         String path="C: // TMP /";

    String[] files = getFiles( path );
    File InFile = null;
    FileReader fr = null;
    BufferedReader br = null;
    int ultimoRegistro=0;

    try {


        proceso=false;


        String anterior="0";
        Connection cn3=DataBaseConnectionProvider.getConnectionFruna();


        String sql3="SELECT * FROM Conc_Cabezera WHERE numero = (SELECT MAX(numero) FROM Conc_Cabezera) ";
        PreparedStatement state3 = cn3.prepareStatement(sql3);
        ResultSet res3=state3.executeQuery();

        if(res3!=null) {
            while(res3.next()) {
                anterior=res3.getString(3);
            }
        }


        //---------------------------------------------

        if ( files != null ) {

        int size = files.length;
        int x;


        for ( x = 1; x <= size; x ++ ) {

            //System.out.println( files[ x-1 ] );                                                                   

                try{
                    fr =new FileReader(files[ x-1 ]);
                    br=new BufferedReader(fr);
                    String sCadena;
                     String cuenta;
                     String año;
                     String n_cartola;
                     String fecha;
                     String detalle;
                     String cargo;
                     String documento;


                     String ano_fecha;
                     String mes_fecha;
                     String dia_fecha;

                     String fechaCom;
                     String sql;
                     String saldo_inicial= null;
                     String saldo_final = null;

                     int cuenta_int=0;
                     int año_int=0;
                     int n_cartola_int=0;
                     int counter=0;
                     int linea=0;
                     int counterBD=0;
                     int numero_cartola=0;
                     String[] cartola=null;
                     String anho = null;
                     Connection cn = null;

                     Date fechaActual1 = new Date();  

                     Connection cn2 = null;
                    cn2 = DataBaseConnectionProvider.getConnectionFruna();
                    String sql2="select count(numero) from conc_cabezera ";
                    PreparedStatement state2 = cn2.prepareStatement(sql2);
                    ResultSet res1=state2.executeQuery();

                    while(res1.next()) {
                        ultimoRegistro=res1.getInt(1)+1;
                    }


                     //Formateando la fecha:
                     DateFormat formatoHora = new SimpleDateFormat("HH:mm:ss S");
                     DateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy");
                     System.out.println("Inicio Proceso: "+formatoHora.format(fechaActual1)+" de fecha: "+formatoFecha.format(fechaActual1));

                    cartola = files[ x-1 ].toString().split("-");
                    numero_cartola = Integer.parseInt(cartola[1].substring(1, cartola[1].length()-4));
                    anho = cartola[0].substring(cartola[0].length()-8, cartola[0].length()-4);
                    fecha = cartola[0].substring(cartola[0].length()-8, cartola[0].length());

                    while(( sCadena=br.readLine())!=null){

                        //System.out.println(sCadena);

                        /*CABECERA 3 PRIMERAS LINEAS*/

                        linea++;
                        if (linea==3) {
                            saldo_final = sCadena.substring(36, 47);
                            //System.out.println(saldo_inicial);

                            sql="insert into Conc_Cabezera values(?,?,?,?,?,?,?);";

                          cn = DataBaseConnectionProvider.getConnectionFruna();
                          PreparedStatement state = cn.prepareStatement(sql);
                          state.setInt(1, ultimoRegistro);
                          state.setInt(2, Integer.parseInt(anho));
                          state.setString(3, saldo_final);
                          state.setString(4, fecha);
                          state.setString(5, anterior);
                          state.setString(6, fecha);
                          state.setString(7,doc);           

                          state.executeUpdate();                                 
                          DataBaseConnectionProvider.close(cn, state, null);


                        }                                                   


                        if (counterBD==0 && linea>3){
                            cn = DataBaseConnectionProvider.getConnectionFruna();       
                        }

                        if (linea>3) {  

                        //sql="insert into Conc_Detalle values("+cuenta+","+año+","+n_cartola+",'"+fechaCom+"','"+detalle+"','"+cargo+"','"+documento+"')";
                          sql="insert into Conc_Detalle values(?,?,?,?,?,?,?,?);";

                          PreparedStatement state = cn.prepareStatement(sql);
                          state.setString(1, doc);
                          state.setInt(2, Integer.parseInt(anho));
                          state.setInt(3, ultimoRegistro);
                          state.setString(4, fecha);
                          state.setString(5, sCadena.substring(51, 96).trim());
                          state.setString(6, sCadena.substring(36, 47).trim());
                          state.setString(7, sCadena.substring(29, 36).trim());
                         state.setString(8, sCadena.substring(96, 97).trim());

                          state.executeUpdate();
                         counterBD++;
                         if (counterBD==50){
                            counterBD=0;
                            DataBaseConnectionProvider.close(cn, state, null);
                         }

                          counter++; 

                        }

                    }
                    proceso=true;
                     //Formateando la fecha:
                    Date fechaActual = new Date();
                    //System.out.println("Inicio Proceso: "+formatoHora.format(fechaActual1)+" de fecha: "+formatoFecha.format(fechaActual1));
                    System.out.println("Fin Proceso: "+formatoHora.format(fechaActual)+" de fecha: "+formatoFecha.format(fechaActual));
                    System.out.println("Lineas: "+counter);


                  }catch(Exception e){
                      proceso = false;
                      System.out.println(e);
                  }                                

            }
        }
     } catch (Exception ex) {
        proceso = false;
        ex.printStackTrace();   
        System.out.println(ex);
    }

    return proceso;

}
public static String[] getFiles( String dir_path ) {

    String[] arr_res = null;

    File f = new File( dir_path );

    if ( f.isDirectory( )) {

        List<String> res   = new ArrayList<>();
        File[] arr_content = f.listFiles();

        int size = arr_content.length;

        for ( int i = 0; i < size; i ++ ) {

            if ( arr_content[ i ].isFile( ))
            res.add( arr_content[ i ].toString( ));
        }


        arr_res = res.toArray( new String[ 0 ] );

    } else
        System.err.println( "¡ Path NO válido !" );


    return arr_res;
}

}

    
asked by Alvaro Muñoz 02.08.2018 в 20:49
source

2 answers

-1

Hello friend what you can do is create the method of reading files and send it to bring as many times as necessary, and for the question of various files you can use threads.

    
answered by 02.08.2018 / 21:09
source
0

If you already have the arrangement with the path of the files that you want to process, you only have to lock your method in one cycle:

for(String archivo: arr_res) //Suponiendo que tu lista con los nombres sea arr_res
    procesaArchivo(archivo);//¿Qué parámetro recibe tu método?

This I put it because your method returns an arrangement, but you mention a list, in case of being a list or an arraylist, modify it

By the way, your Load Reconciliation class does not have a constructor, you should add it.

    
answered by 02.08.2018 в 21:33