Huỳnh Phong Lưu
Huỳnh Phong Lưu

Reputation: 1

ReadFile in TextArea JAVA

I want the textarea show the content of a file txt. Here is my code, it just shows the last line of the text, I know the problem is ta1.settext(). Can someone pls give me some advice?

public void actionPerformed(ActionEvent arg0) {
    if(arg0.getSource() == btnOK) {
        String link = tf1.getText().toString();
        try(BufferedReader br = new BufferedReader(new FileReader(link)))
        {
            String line;
            while((line = br.readLine()) != null) {
                ta1.setText(line);
            }
            br.close();
        }

all the line of the file The last line

Upvotes: 0

Views: 91

Answers (1)

jack jay
jack jay

Reputation: 2503

you are resetting your TextArea text by the latest line read from your file. While your need is to append all the lines read and show them.

while((line = br.readLine()) != null) {
      ta1.append(line);
}

Upvotes: 1

Related Questions