开发者

How to read a text file into jtextarea in Java Swing

Here 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);
}

And my .txt file contains the following:

contig00001 length=586 numreads=4 CGGGAAATTATCcGCGCCTTCACCGCCGCCGGTTCCACCGACGAACGGATACTGCGtGaa ggCCGCGATCCCGTCggaCGGAAAaCGCCcTGGCCCGGGAaCATACCGTTCGGGCCGCCA AGTGTTATAGCCGGACCACTTGTCAGAACATTTCCaaTCCGAAGATGTGAGTtCGGAAGg TAAAAGCCCGACAAGTTGCGCGgTGAATTTACCTTtACcGCACGATATGCGTCCGTATTA AaGAAAaGTTCGAAATTATCAGTAAGGCCGACCTGAAaGCTGACCGGGAGTTCAACAAAA TCTGCATCACCcGGgTCACGGTCGAAATTGCTGTACGCGGCGCTGAACGTAAATTCACCC TTTcTAAGGGTGTCGCcGTCGTAAACCGTAAaCAaGCCGGTAGCGCCGCCCA开发者_如何转开发TCGGGCCG CCGGTACCAACCGTCGGTGCCGTGTTTCTtGCATCATTGTCCGATCGAGCGTTCTCGTCC GCTTGTGCAAaTCCTGCAaTAGCTAACGTGAAAACGATCAGAGCTGTTGTAAATACTCTA TAAGCGAGATTCATCACATTCCTCcGCCGAAATAAAAAGTTAATTt

contig00002 length=554 numreads=4 TGCGCCAaCCGCGCTCTtCATAAaTGGGCACTGCTCCCGATGGCCgACTCGGGCGGTTCG CCATGAGATCTTTGCCtACCcAGgAaCtCACcACCAAGTCTGATTGCTGTGTGTTTtCTT CAAGTCCCTATTTCTATTCtCTTtAATGGAACCCGTAGGAAACCCGTGTAGGACGCGGGA aCCGCACTTgAAGGGGGAGGCGCGGGGTACCGGtCCGGGAACGTACGGGTACCGGCGGGG gAGGGGAGGGGGACCgCTCCGGGAAGGCCAGGGGACGGATTGGGGAAGGgCGGGTACCGA AGCGGGgAAaTGGGggAaCcGGCGAGAGGGTTCCTCGCTAAGTGGGGGAAATaGGGGAAA GGTTGACCAGTGGTtCCCcGCTCTCGTAACATGCCTCAGATAGCGCCATCCGCTGTACCT GGtcaggtcGctggcaacttcggccgagcaggtgaacccgaaaggtgagggtcagtgtga cacaccaaccgaacaccgacgaggcaagcgtaggagccggcgtggccgcgcccggcggcg ctgaggactcctcg

But shows the output by skipping the first two lines.

What is the reason for this?


You don't need the while loop, or the readLine method. Just call jtextArea1.read(reader, "jTextArea1")

Edit: update following your comment. If you want to skip all lines starting with >, you will need to read the file manually and then append each line to your textArea.

So something like:

String line;
while ((line = reader.readLine()) != null)
{
    if (!line.startsWith(">"))
    {
        jTextArea.append(line + "\n");
    }
}


Use:

FileReader reader = new FileReader("filename.txt");
txtarea.read(reader, "filename.txt"); //Object of JTextArea

You need only the above two lines to read from a file and put it into JTextArea...


The problem must have been solved by the time, yet there's still no answer to the question why the first two lines are skipped.

You create reader and then read the first two lines from the file, remaining lines are loaded into jTextArea1.

Your code:

/* 1 */ while((textLine=reader.readLine())!=null){
/* 2 */     textLine = reader.readLine();
/* 3 */     jTextArea1.read(reader,"jTextArea1");
        } 

Line 1 reads the first line from the file. Then in the body of while you read the second line from the file at line 2. Line 3 reads the rest of the file into jTextArea1.

On the next iteration of the while loop, reader.readLine() returns null since the file is completely read.


To load text in a JTextComponent use its read method as suggested by Phill and Bhushankumar.

The second parameter to read is not used by JTextArea, so it's safe to pass null. This second parameter is usually used to store to URL of the loaded file to resolve relative references, for example links in an HTMLDocument.


textLine = reader.readLine(); is called twice...

Fixed:

try {
    String textLine;
    FileReader fr = new FileReader("ad.txt");
    BufferedReader reader = new BufferedReader(fr);

    while((textLine=reader.readLine()) != null){
        // textLine = reader.readLine(); // Remove this line
        jTextArea1.read(reader, "jTextArea1");
    }
}
catch (IOException ioe) {
    System.err.println(ioe);
    System.exit(1);
}


Correctly is:

try {
    FileReader fr = new FileReader("tablica.txt");
    BufferedReader reader = new BufferedReader(fr);

    do {
        l.read(reader, null);
    }

    while ((textLine=reader.readLine()) != null)
        ;

}

catch (IOException ioe) {
    System.err.println(ioe);
    System.exit(1);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜