开发者

Using FileReader only displays Last Line of Text File?

开发者 https://www.devze.com 2023-02-23 03:57 出处:网络
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 li

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){
   开发者_JAVA技巧     JOptionPane.showMessageDialog(this, "Data not read");
    }


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


I think You are overriding the line variable.

line+=...

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


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

精彩评论

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