使用 FileReader 只显示文本文件的最后一行?

发布于 2024-10-31 08:08:16 字数 1555 浏览 8 评论 0原文

我正在尝试在 GUI 中创建的 JTextArea 中显示文本文件中的信息。我已经弄清楚如何将信息从文件获取到 JTextArea,但它只是获取文件的最后一行。我需要显示所有行。我不断地改变循环,但无法弄清楚这一点。任何帮助将不胜感激。看一下我的代码:

public TextArea() {
    initComponents();


    try {
        FileReader one = new FileReader ("info.txt");
        BufferedReader buf = new BufferedReader(one);

        String line = "";
        StringTokenizer st = null;
        int lineNumber = 0, tokenNumber = 0;
        //textArea.setText(line);

        while ((line = buf.readLine()) != null) {
            lineNumber++;

            //break comma separated line using ","
            st = new StringTokenizer(line, ",");

            while (st.hasMoreTokens()) {
                //display csv values
                tokenNumber++;
                line = ("Title: " + st.nextToken()
                        + "\n" + "Make:" + st.nextToken()
                        + "\n" + "Model:" + st.nextToken()
                        + "\n" + "Year:" + st.nextToken()
                        + "\n" + "Price:" + st.nextToken()
                        + "\n" + "Notes:" + st.nextToken()
                        + "\n" + "Details:" + st.nextToken()
                        + "\n");

                textArea.setText(line);
            }

            //reset token number
            tokenNumber = 0;
            //textArea.setText(line);
        }

    } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(this, "File not found");
    } catch (IOException e){
        JOptionPane.showMessageDialog(this, "Data not read");
    }

I'm trying to display the information from a text file in a JTextArea I've created in a GUI. I've figured out how to get the info from the file to the JTextArea, but it's only grabbing the last line of the file. I need to display all of the lines. I keep changing the loop around, but can't figure this one out. Any help would be greatly appreciated. Here's a look at my code:

public TextArea() {
    initComponents();


    try {
        FileReader one = new FileReader ("info.txt");
        BufferedReader buf = new BufferedReader(one);

        String line = "";
        StringTokenizer st = null;
        int lineNumber = 0, tokenNumber = 0;
        //textArea.setText(line);

        while ((line = buf.readLine()) != null) {
            lineNumber++;

            //break comma separated line using ","
            st = new StringTokenizer(line, ",");

            while (st.hasMoreTokens()) {
                //display csv values
                tokenNumber++;
                line = ("Title: " + st.nextToken()
                        + "\n" + "Make:" + st.nextToken()
                        + "\n" + "Model:" + st.nextToken()
                        + "\n" + "Year:" + st.nextToken()
                        + "\n" + "Price:" + st.nextToken()
                        + "\n" + "Notes:" + st.nextToken()
                        + "\n" + "Details:" + st.nextToken()
                        + "\n");

                textArea.setText(line);
            }

            //reset token number
            tokenNumber = 0;
            //textArea.setText(line);
        }

    } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(this, "File not found");
    } catch (IOException e){
        JOptionPane.showMessageDialog(this, "Data not read");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁与争疯 2024-11-07 08:08:16

查看您的代码:

while (st.hasMoreTokens()) {
            //display csv values
            tokenNumber++;
            line = ("Title: " + st.nextToken()
                    + "\n" + "Make:" + st.nextToken()
                    + "\n" + "Model:" + st.nextToken()
                    + "\n" + "Year:" + st.nextToken()
                    + "\n" + "Price:" + st.nextToken()
                    + "\n" + "Notes:" + st.nextToken()
                    + "\n" + "Details:" + st.nextToken()
                    + "\n");

            textArea.setText(line);
        }

每次找到新令牌时,您都将 textarea val 设置为最后找到的令牌。
所以显然文本区域只会显示最后一行。
你可以尝试这样的事情:

textArea.setText(textArea.getText() + line);

Look at your code:

while (st.hasMoreTokens()) {
            //display csv values
            tokenNumber++;
            line = ("Title: " + st.nextToken()
                    + "\n" + "Make:" + st.nextToken()
                    + "\n" + "Model:" + st.nextToken()
                    + "\n" + "Year:" + st.nextToken()
                    + "\n" + "Price:" + st.nextToken()
                    + "\n" + "Notes:" + st.nextToken()
                    + "\n" + "Details:" + st.nextToken()
                    + "\n");

            textArea.setText(line);
        }

Everytime you find a new token you set the textarea val to last token found.
So obviously text area will display only last line.
You can try something like:

textArea.setText(textArea.getText() + line);
暖阳 2024-11-07 08:08:16

我认为你正在覆盖 line 变量。

line+=...

连接,然后在循环外设置整行连接的值。

I think You are overriding the line variable.

line+=...

Concatenate, and then set the value of the whole line concatenated outside the loop.

好菇凉咱不稀罕他 2024-11-07 08:08:16
while (st.hasMoreTokens()) {
            //display csv values
            tokenNumber++;
            line = line +"\n"+("Title: " + st.nextToken()
                    + "\n" + "Make:" + st.nextToken()
                    + "\n" + "Model:" + st.nextToken()
                    + "\n" + "Year:" + st.nextToken()
                    + "\n" + "Price:" + st.nextToken()
                    + "\n" + "Notes:" + st.nextToken()
                    + "\n" + "Details:" + st.nextToken()
                    + "\n");


        }
 textArea.setText(line);
while (st.hasMoreTokens()) {
            //display csv values
            tokenNumber++;
            line = line +"\n"+("Title: " + st.nextToken()
                    + "\n" + "Make:" + st.nextToken()
                    + "\n" + "Model:" + st.nextToken()
                    + "\n" + "Year:" + st.nextToken()
                    + "\n" + "Price:" + st.nextToken()
                    + "\n" + "Notes:" + st.nextToken()
                    + "\n" + "Details:" + st.nextToken()
                    + "\n");


        }
 textArea.setText(line);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文