Separate content from the row in the BufferedReader loop

4

I have txt files with approximately 5,000 items, and these do not have a default, for example:

10 20
30
40

50 60 70

80
90 100

I need to import this to SQLite , each value being 1 register. Example:

ID | VALOR
 1 |   10
 2 |   20
 3 |   30
 4 |   40
 5 |   50
 6 |   60
 7 |   70
 8 |   80
 9 |   90
10 |  100

I read txt as follows:

try {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            text.append(line);
            text.append("\n");
        }
        bufferedReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

In place of text.append(line); and text.append("\n"); , would I have as long as I read each line, already "treat / detach" the values that are on the same line, so I can already bring separate?

If I have 1 value on the line, I can already insert it directly into the bank, for example:

insere(line);

But what about multiple values on the same line? For example:

The line with value 50 60 70 , would I already break the 3 values, and execute on a method to include in the bank?

If I use a line = line.replace(" ", "\n"); before append , it will bring me the line in soft, and I can deal with if to insert or not. Would this be the best solution?

The idea is to do this right in reading, so you do not have to create a huge variable, treat the content, and then do insert .     

asked by anonymous 26.04.2018 / 21:26

1 answer

2

Make the split of line and treat each element of the array.

Anything like this:

while ((line = bufferedReader.readLine()) != null) {
    String[] values = line.split(" ");
    for(String value: values) {
        insere(value);
    }
}

See a simulation at Ideone.

    
26.04.2018 / 23:06