Error when tracing data from a txt in java

0

I have a problem when going through a file .txt I can read it perfectly but there is a part of that file that I want to skip and start reading from another point so the file starts

Relatório por Data

31/08/16 MEZCLA III       13:36:57 13:43:22 0                      6,24T

so just as the file is pasted I want to omit the relational part by data and start reading from the date this is my method in java

   public void init() {
    File archivo = null;
    FileReader fr = null;
    BufferedReader br = null;
    Date myDate = new Date();

    String resultado = new SimpleDateFormat("ddMMyyy").format(myDate);
    try {
        // Apertura del fichero y creacion de BufferedReader para poder
        // hacer una lectura comoda (disponer del metodo readLine()).
        archivo = new File("C:\texto\" + resultado + ".txt");
        fr = new FileReader(archivo);
        br = new BufferedReader(fr);

        // Lectura del fichero
        String linea;

        while ((linea = br.readLine()) != null) {
            columns = linea.split(" ");
            String fechaC = String.valueOf(columns[0]);
            System.out.println(fechaC);

         }


    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // En el finally cerramos el fichero, para asegurarnos
        // que se cierra tanto si todo va bien como si salta 
        // una excepcion.
        try {
            if (null != fr) {
                fr.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}

At the moment of doing split just I take the index 0 if I put 1 throw me an exception of Arrayindex

    
asked by Alexander Villalobos 23.06.2017 в 18:51
source

1 answer

0

You have an error in the logic of the program, the exception is because the second line of the file is a blank line, inside your loop while you try to read the first value that you have in the array, the second line only has one value and that's why when you want to read the second your program sends an exception. If you can assure that your text file always has the same format, you can do what SJuan76 suggested and read the first two lines before the while.

br.readLine();
br.readLine();

while()
{
    //Procesar texto
}
    
answered by 23.06.2017 / 19:13
source