简体   繁体   中英

Read files and insert data count into JTable

How to read files and insert data count into JTable ?

I have n number of text files. What I need to do is to read data from each file and insert the data count of each corresponding file into Java table such that:

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

Can you please help me out with some ideas to achieve the same?

Arrange for your read method to accept a File and return 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;
}

Once you have the Map , you can build a TableModel around it, as shown in this 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()]);
    }
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM