簡體   English   中英

如何使用文本文件中的數據創建 DefaultTableModel

[英]How create a DefaultTableModel with data from text file

我有這個測試文件text_file我的表格視圖是在此處輸入圖像描述我想要適合每一行的每一列(first_row-first_column、second_row-second_column 等。)錯誤在哪里?

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

}

讀取文件的問題,因為您逐行讀取而不是讀取所有行

        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

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM