I’m trying to open a file with Jfilechooser and read with Bufferedreader but is only reading the last line

Asked

Viewed 33 times

1

here is the code

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;

public class Layout extends JFrame {
    JTextArea ta = new JTextArea();
    JMenuBar menu = new JMenuBar();
    JMenu arq = new JMenu("Arquivo");
    JMenuItem abrir = new JMenuItem("Abir");
    public Layout(){
        setSize(500,500);
        setLayout( new GridLayout(1,1));
        setJMenuBar(menu);
        add(ta);
        menu.add(arq);
        arq.add(abrir);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        abrir.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "txt", "txt");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                   String caminho = chooser.getSelectedFile().getPath();
                      try(BufferedReader br = new BufferedReader(new FileReader(caminho))){
                           String linha = br.readLine();
                        while (linha!=null){
                            ta.setText(linha);
                            linha = br.readLine();
                        }

                    }catch (Exception exception){
                        System.out.println(exception.getMessage());
                    }
                }

            }
        });

    }
}

1 answer

1


It is reading the entire file yes. The problem is that you overwrite what has already been read and only use the last line read:

String linha = br.readLine();
while (linha!=null){
    ta.setText(linha); // mudo o texto para a última linha lida
    linha = br.readLine(); // lê outra linha
}

That is, you change the text of JTextArea, then reads another line, changes the text to this line, reads another line, changes the text to that line, reads another line, etc. At the end it will have the value of the last line.

If the idea is to have all the contents of the file on textarea, then first you read everything and at the end you change the text:

String linha = br.readLine();
while (linha != null) {
    linha += br.readLine();
}
ta.setText(linha);

Although, to concatenate strings into one loop, the most appropriate is to use a StringBuilder:

StringBuilder sb = new StringBuilder();
String linha;
while ((linha = br.readLine()) != null) {
    sb.append(linha);
}
ta.setText(sb.toString());

The detail is that the readLine does not bring line breaks from the file. If you want to include them, put sb.append(linha).append("\n") within the while.

  • Thank you so much for your help, it worked.

Browser other questions tagged

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