0

如何读取文件并将数据计数插入JTable

我有 n 个文本文件。我需要做的是从每个文件中读取数据并将每个相应文件的数据计数插入到 Java 表中,这样:

File Name          Total records exist
-----------------------------------------
x1.txt                    457
x2.txt                    876
.                         .
.                         .
.                         .
xn.txt                    345
-----------------------------------------
Total                     1678
-----------------------------------------

你能帮我一些想法来实现同样的目标吗?

4

1 回答 1

1

安排您的 read 方法接受 aFile并返回 a Map<String, Integer>

private Map<String, Integer> readData(File file) {
    Map<String, Integer> map = new HashMap<String, Integer>();
    // fill in the map from the file
    return map;
}

一旦你有了Map,你可以TableModel围绕它构建一个,如下所示EnvTableTest

private static class FileDataModel extends AbstractTableModel {

    private Map<String, Integer> data = readData(file);
    private String[] keys;

    public FileDataModel() {
        keys = data.keySet().toArray(new String[data.size()]);
    }
    ...
}
于 2013-05-10T14:10:13.783 回答