如何使用文本文件中的数据创建一个DefaultTableModel。

huangapple 未分类评论54阅读模式
英文:

How create a DefaultTableModel with data from text file

问题

BufferedReader infile = new BufferedReader(reader);
String line = "";
int counter = 0;
String title = "";
String author = "";
String price = "";
try {
    while ((line = infile.readLine()) != null) {
        ++counter;

        if (counter == 1) {
            title = line;
        } else if (counter == 2) {
            author = line;
        } else if (counter == 3) {
            price = line;
            SimpleBook sb = new SimpleBook(title, author, price);
            bookList.add(sb);
            counter = 0;
        }
    }
} catch (IOException ex) {
    Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
}
英文:

I have this test file text_file
My table view is [enter image description here][2]
1: https://i.stack.imgur.com/fG6pz.png
[2]: https://i.stack.imgur.com/6b6uP.png
I want suit each row with each column(first_row-first_column,second_row-second_column, etc..) Where is error?

        BufferedReader infile = new BufferedReader(reader);
        String line = "";
        int counter = 0;
        String title = "";
        String author = "";
        String price = "";
        try {
            while ((line  = infile.readLine()) != null) {
                ++counter;
               
                if (counter == 1) {
                    title = line;
                } else if (counter == 2) {
                    author = line;
                } else if (counter == 3) {
                    price = line;
                    SimpleBook sb = new SimpleBook(title, author, price);
                    bookList.add(sb);
                    counter = 0;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

答案1

得分: 0

文件读取问题因为你是逐行读取而不是读取所有行

```java
        int counter = 0;
        String title = "", author = "";
        double price = 0.0;
        while ((line = infile.readLine()) != null) {
            ++counter;
            if (counter == 1)
                title = line;
            else if (counter == 2)
                author = line;
            else if (counter == 3) {
                price = Double.parseDouble(line);
                SimpleBook sb = new SimpleBook(title, author, price);
                bookList.add(sb);
                counter = 0;
            }
        }

,或者你可以查看来自Oracle文档readAllLines方法。


<details>
<summary>英文:</summary>

The problem in reading the file, because you read line by line not read all lines

    int counter = 0;
    String title = &quot;&quot;, author = &quot;&quot;;
    double price = 0.0;
    while ((line = infile.readLine()) != null) {
        ++counter;
        if (counter == 1)
            title = line;
        else if (counter == 2)
            author = line;
        else if (counter == 3) {
            price = Double.parseDouble(line);
            SimpleBook sb = new SimpleBook(title, author, price);
            bookList.add(sb);
            counter = 0;
        }
    }

, or you can check **readAllLines** from [Oracle doc][1]


  [1]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#readAllLines(java.nio.file.Path)

</details>



# 答案2
**得分**: 0

```java
public class SimpleBookList {

    private ArrayList<SimpleBook> bookList;

    public SimpleBookList() {
        bookList = new ArrayList<SimpleBook>();
    }

    public void add(SimpleBook sb) {
        bookList.add(sb);
    }

    public ArrayList<SimpleBook> getBooks() {
        return bookList;
    }

    public void readFromTxt(String filename) {
        File file = new File(filename);
        FileReader reader = null;
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            System.exit(1);
        }
        BufferedReader infile = new BufferedReader(reader);
        String line = "";
        int counter = 0;
        String title = "";
        String author = "";
        String price = "";
        try {
            while ((line  = infile.readLine()) != null) {
                ++counter;
                if (counter == 1) {
                    title = line;
                } else if (counter == 2) {
                    author = line;
                } else if (counter == 3) {
                    price = line;
                    SimpleBook sb = new SimpleBook(title, author, price);
                    bookList.add(sb);
                    counter = 0;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
英文:
    public class SimpleBookList {

    private ArrayList&lt;SimpleBook&gt; bookList;

    public SimpleBookList() {
        bookList = new ArrayList&lt;SimpleBook&gt;();
    }

    public void add(SimpleBook sb) {
        bookList.add(sb);
    }

    public ArrayList&lt;SimpleBook&gt; getBooks() {
        return bookList;
    }

    public void readFromTxt(String filename) {
        File file = new File(filename);
        FileReader reader = null;
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            System.exit(1);
        }
        BufferedReader infile = new BufferedReader(reader);
        String line = &quot;&quot;;
        int counter = 0;
        String title = &quot;&quot;;
        String author = &quot;&quot;;
        String price = &quot;&quot;;
        try {
            while ((line  = infile.readLine()) != null) {
                ++counter;
                if (counter == 1) {
                    title = line;
                } else if (counter == 2) {
                    author = line;
                } else if (counter == 3) {
                    price = line;
                    SimpleBook sb = new SimpleBook(title, author, price);
                    bookList.add(sb);
                    counter = 0;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

huangapple
  • 本文由 发表于 2020年5月30日 00:51:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/62090905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定