How to read text files into jtextarea in Java Swing

This is my code:

try {
    String textLine;
    FileReader fr = new FileReader("ad.txt");
    BufferedReader reader = new BufferedReader(fr);
    while((textLine=reader.readLine()) != null) {
        textLine = reader.readLine();
        jTextArea1.read(reader,"jTextArea1");
    }
}
catch (IOException ioe) {
    System.err.println(ioe);
    System.exit(1);
}

my Txt file contains the following:

But skip the first two lines to display the output

What's the reason?

Solution

You do not need a while loop or a readLine method Just call jtextarea1 read(reader,“jTextArea1”)

Edit: update according to your comments If you want to skip all lines starting with >, you need to read the file manually and attach each line to textarea

So it's similar to:

String line;
while ((line = reader.readLine()) != null)
{
    if (!line.startsWith(">"))
    {
        jTextArea.append(line + "\n");
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>