Capture txt file row by row and play in a txtbox

Asked

Viewed 849 times

0

good afternoon need to take 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.

1 answer

0


The problem is that you are assigning the content of each line to Textview within the cycle while. Each time a new assignment is made restore what the previous one had assigned.

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

Before starting reading, outside the loops:

StringBuilder sb = new StringBuilder();

During reading, inside the loop:

sb.append(linha));

At the end of the reading, outside the loops:

txtcoluna1.setText(sb.toString());
  • Good afternoon, thanks for the reply, I fixed the code but I still have the same problem; Bufferedreader br = null; Try ' br = new Bufferedreader (new Filereader ("/sdcard/Aperamapps/DBQ/Dbqmaster/data.txt"); for (String line = br.readline(); line != null; line = br.readline()){ Stringbuilder Sb = new Stringbuilder(); Sb.append(line); txtcoluna1.setText(Sb.toString()); tch (Ioexception e) { e. printStackTrace(); }

  • The txtcoluna1.setText(sb.toString()); has to be put out of the loops at the end.

  • Ok, now txtcoluna1 displays the entire text file, starting from item 1 to item 10. How do I filter this data, for example: My application has 10 txtedit (txtcoluna1, txtcoluna2, txtcoluna3...txtconlunaN), and the text file has 10 lines, as I put line 1 to be displayed in txtcoluna1, line 2 in txtcoluna2 ?

  • Use a Arraylist<String> instead of Stringbuilder.

Browser other questions tagged

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