Capture file txt line by line and play in a txtbox

0

Good afternoon I need to get the value of a text file and write each line in a txtedit, the file is generated by the code below:

byte[] dados;


             File arquivo = new File(Environment.getExternalStorageDirectory(), "/AperamApps/DBQ/DBQmestre/dados.txt");


             try {

             if (!arquivo.exists()) {
             //cria um arquivo (vazio)
             arquivo.createNewFile();
             }

             //caso seja um diretório, é possível listar seus arquivos e diretórios
             File[] arquivos = arquivo.listFiles();

             //escreve no arquivo
             FileWriter fw = new FileWriter(arquivo, true);

             BufferedWriter bw = new BufferedWriter(fw);

                 bw.write("#1 "+as1);
                 bw.newLine();            
                 bw.write("#2 "+as2);
                 bw.newLine();     
                 bw.write("#3 "+as3);
                 bw.newLine();           
                 bw.write("#4 "+as4);
                 bw.newLine();
                 bw.write("#5 "+as5);
                 bw.newLine();
                 bw.write("#6 "+as6);
                 bw.newLine();
                 bw.write("#7 "+as7);
                 bw.newLine();
                 bw.write("#8 "+as8);
                 bw.newLine();
                 bw.write("#9 "+as9);
                 bw.newLine();
                 bw.write("#10 "+as10);
                 bw.newLine();

             bw.close();
             fw.close();              
             //faz a leitura do arquivo
             FileReader fr = new FileReader(arquivo);
             BufferedReader br = new BufferedReader(fr);              
             //equanto houver mais linhas
             while (br.ready()) {
             //lê a proxima linha
             String linha = br.readLine(); 
             //faz algo com a linha
             System.out.println(linha);
             }
             br.close();
             fr.close();   
             } catch (IOException ex) {
             ex.printStackTrace();
             }    

And here's the code I'm trying to use to read the file:

     BufferedReader br  = null;  
    try {  
        br = new BufferedReader (new FileReader ("/sdcard/AperamApps/DBQ/DBQmestre/dados.txt"));  
        for (String linha = br.readLine(); linha != null; linha = br.readLine()){

            while ((linha = br.readLine()) != null) {
                System.out.println(linha);

                br.readLine();

                txtcoluna1.setText(linha);
            }



        }  
 br.close();
 } catch (IOException e) {
     e.printStackTrace();
 }

The code can even read, but it always displays the last line of the text file.

    
asked by anonymous 22.10.2015 / 19:41

1 answer

0

The problem is that you are assigning the contents of each line to the TextView within the while cycle. Each time a new assignment is made, the new assignment replaces the previous assignment.

One solution is to use a StringBuilder to build the text and at the end of the reading assign the result to the TextView.

Before you start reading, out of loops :

StringBuilder sb = new StringBuilder();

While reading, within the loop :

sb.append(linha));

At the end of the reading, outside loops :

txtcoluna1.setText(sb.toString());
    
22.10.2015 / 20:16