简体   繁体   English

输入流阅读器,带有多行文本文件

[英]Input Stream Reader with multi-line text file

My program can't read a txt file that is in a multi-line format. 我的程序无法读取多行格式的txt文件。 The content of the text file should be printed inside a textArea but when it comes to multi-lined files nothing happens. 文本文件的内容应打印在textArea内,但涉及多行文件时则什么也没有发生。 Also I would like to prompt a message : "File Existed" when a file exists, and "File not found" when a file don't exist. 我也想提示一条消息:文件存在时为“ File Existed”,文件不存在时为“ File not found”。

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. 您可能要使用的是append方法。

Also you are not closing your resources in right place, since it should be done only in finally block. 同样,您没有在正确的位置关闭资源,因为仅应在finally块中完成。 Consider using try-with-resources which will handle it for you. 考虑使用try-with-resources将为您处理它。

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. 将此TextComponent的文本设置为指定的文本。 If the text is null or empty, has the effect of simply deleting the old text. 如果文本为null或为空,则可以简单地删除旧文本。

textArea.setText(sCurrentLine) overwrites the entire text of the text area for every line. textArea.setText(sCurrentLine)覆盖每行文本区域的整个文本。 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将整个文件读入...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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