2013-05-10 131 views
0

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

我有n个文本文件。我需要做的是从每个文件中读取数据,并插入每个对应文件的数据计数到Java台,使得:

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

能否请你帮我出一些想法来达到同样的?

+0

*“能否请您帮助我的一些想法来达到同样的?” * 1)参见[开始编写程序(http://home.earthlink.net/~patricia_shanahan/beginner.html )为伟大的技巧。 2)[你有什么尝试?](http://www.whathaveyoutried.com/)我的意思是*除了*问我们。 – 2013-05-10 10:33:45

+0

@AndrewThompson:我正在尝试使用Hashmap,在读取每行时读取缓冲区读取器并增加计数,然后尝试将最终计数值放入hashmap中。 – 2013-05-10 10:40:17

+0

*“我正在尝试......”*用[SSCCE](http://sscce.org/)解释更好。 – 2013-05-10 10:41:21

回答

1

安排您的阅读方法接受File并返回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()]); 
    } 
    ... 
} 
相关问题