简体   繁体   English

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

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

I have this test file text_file My table view is enter image description here I want suit each row with each column(first_row-first_column,second_row-second_column, etc..) Where is error?我有这个测试文件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);
        }
    }

}

The problem in reading the file, because you read line by line not read all lines读取文件的问题,因为您逐行读取而不是读取所有行

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

, or you can check readAllLines from Oracle doc ,或者您可以从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