How to read multiple lines from a text file (.txt) and store them in the same variable?

Asked

Viewed 1,278 times

-1

Someone has an idea to help me extract the lines below and that always present themselves in this pattern?

Palavra1 (é sempre igual)
Linha a ser extraida, de tamanho variavel
Linha a ser extraida, de tamanho variavel
Linha a ser extraida, de tamanho variavel
Palavra2 (é sempre igual)

bla bla bla
bla bla bla

Palavra1 (é sempre igual)
Linha a ser extraida, de tamanho variavel
Linha a ser extraida, de tamanho variavel
Linha a ser extraida, de tamanho variavel
Palavra2 (é sempre igual)

The 3 lines are parts of the same information that I should store in a single variable and display to the user, but that, at the time of file conversion, were thrown in 2, 3 or 4 lines.

Follow the code I imagined so far:

StringBuilder sb = new StringBuilder();
if(linha.startsWith("palavra1")){
    linha = arquivoEntrada.readLine() + 1; //pula a linha
    if(!linha.startsWith("palavra2")){ //minha condição de parada
        sb.append(linha + " "); //StringBuffer pra ir armazenando as linhas lidas
                                //como fazer o sistema pular a linha agora??????
    } else {

    }
    variavelQualquer = sb.toString();
}

With this code, I can only get the first line. I tried with while instead of the second if, but it didn’t work.

Any help would be welcome.

  • Can you post any more snippets of code? I think it would help to understand what you’re doing and propose something closer to what you need.

  • Briefly, I had a PDF file that was converted to txt. My job is to manipulate via substring certain information from this generated TXT and finally create another final TXT file displaying only the manipulated information. In this case, these 3 or more lines that I illustrated should be in a single line (ie, they are the information I need), but the PDF converter p/ TXT ended up breaking and playing in several lines. I need now, then, to reunite them again and play in the final TXT as a single line. I hope it has become clearer now.

  • The absence of at least one cycle makes me see that you should only read the first line.

  • @Cold, I tried to use the while, but it burst of memory. Anyway, the if should work if my logic is correct.

  • @bigown My logic was to try to work with the regularity that allows me to identify the block of lines that I need, and that regularity is the existence of the same word before those lines and another word after those lines. I mean, the lines I need are always on a block bounded by two words that are always the same.

1 answer

1

I suggest the following:

...
StringBuilder sb = new StringBuilder();
linha = arquivoEntrada.readLine();
do 
{
    if (linha.startsWith("Palavra1"))//verifica se começa pela Palavra e não
    {
        String linhaAux = arquivoEntrada.readLine();
        while (linhaAux != null && !linhaAux.startsWith("Palavra2"))
        {
            sb.append(linhaAux + " ");
            linhaAux = arquivoEntrada.readLine();
        }
    }
    linha = arquivoEntrada.readLine();
} while (linha != null)
...
  • Your idea kind of worked, but enough so I can try walking alone. I can’t upvote because I don’t have 15 reputation points. Anyway, I really appreciate it.

  • No makas. Tell me what it means more or less :) ?

  • Hehe actually worked. He concreted the lines as I needed. The "more or less" is because it has returned not only the concatenated lines, but has generated several blank lines between each of the concatenated lines. I’m still trying to figure out why that is and eliminate these blank lines. Anyway, the lines that matter are concatenated and I thank you again so much.

  • Blank lines? Um. I don’t see how, yet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.