简体   繁体   中英

Input Stream Reader with multi-line text file

My program can't read a txt file that is in a multi-line format. The content of the text file should be printed inside a textArea but when it comes to multi-lined files nothing happens. Also I would like to prompt a message : "File Existed" when a file exists, and "File not found" when a file don't exist.

Here's my code :

BufferedReader br = null;
try {
    String sCurrentLine;

    br = new BufferedReader(new FileReader("C:\\Users\\theBeard\\workspace\\LeapYear\\"+textField.getText()));

    while ((sCurrentLine = br.readLine()) != null) {
        textArea.setText(sCurrentLine);
    }
        br.close();
} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    try {
        if (br != null)br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

also , is this method the correct to check if a file exists?

try {

                String sCurrentLine;

                br = new BufferedReader(new FileReader("C:\\Users\\theBeard\\workspace\\LeapYear\\"+textField.getText()));
                textArea.read(br, textArea);//this was a suggestion     by     someone below


                while ((sCurrentLine = br.readLine()) != null) {



                     textArea.append(sCurrentLine);
                     textArea.append(System.lineSeparator());
                }

            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (br != null)
                    {

                        br.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

textArea.setText(sCurrentLine); doesn't add text to area, but replaces it with new text. So your code will set text to last line from your text file. So if your last line is empty you will not see anything.

What you may want to use is append method.

Also you are not closing your resources in right place, since it should be done only in finally block. Consider using try-with-resources which will handle it for you.

So try with something like

try (BufferedReader br = new BufferedReader(
        new FileReader("C:\\Users\\theBeard\\workspace\\LeapYear\\"+ textField.getText()))) {
    String sCurrentLine;
    while ((sCurrentLine = br.readLine()) != null) {
        textArea.append(sCurrentLine);
        textArea.append(System.lineSeparator());
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

附加文本,而不是为每次迭代设置文本

textArea.append(sCurrentLine + System.getProperty("line.separator"));
public void setText(String t)

Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text.

textArea.setText(sCurrentLine) overwrites the entire text of the text area for every line. If your last line in the file is an emtpy line, your text area will be empty.

Well, if your last line is empty, the only thing that will be set is this empty line, since you always overwrite the text in your text area. I would suggest using a StringBuilder to read the whole file into...

StringBuilder sb = new StringBuilder();
... read via BufferedReader
sb.append( sCurrentLine );

...set whole text...
textArea.setText(sb.toString());
while ((sCurrentLine = br.readLine()) != null) {
    textArea.setText(sCurrentLine);
}

This over-writes the existing value everytime, so you end up only having the last line value.

Get the previous value and then append to it.

while ((sCurrentLine = br.readLine()) != null) {
    String x = textArea.getText().toString();
    textArea.setText(x + sCurrentLine);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM