所以,在我看來,你有你需要回答四個問題,這就違背了問一個問題的現場禮儀,但會給它一個鏡頭
- 如何列出了一系列文件的,大概使用某種過濾
- 如何以某種有意義的方式讀取文件和處理數據
- 如何管理數據結構中的數據
- 在0123顯示的數據。
清單文件
大概列出文件的最簡單的方法是使用File#list
並傳遞FileFilter
滿足您的需求現在
File[] files = new File(".").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().toLowerCase().startsWith("janurary");
}
});
,我會寫這花了File
的方法代表您要列出的目錄的對象以及使用FileFilter
進行搜索的對象...
public File[] listFiles(File dir, FileFilter filter) throws IOException {
if (dir.exists()) {
if (dir.isDirectory()) {
return dir.listFiles(filter);
} else {
throw new IOException(dir + " is not a valid directory");
}
} else {
throw new IOException(dir + " does not exist");
}
}
這樣,您可以根據不同的FileFilter
s搜索多個不同的文件集。
當然,你也可以使用新的API Paths/Files
查找文件以及
閱讀文件...
讀取多個文件歸結爲同樣的事情,讀一個單一的文件...
// BufferedReader has a nice readline method which makes
// it easier to read text with. You could use a Scanner
// but I prefer BufferedReader, but that's me...
try (BufferedReader br = new BufferedReader(new FileReader(new File("...")))) {
String line = null;
// Read each line
while ((line = br.readLine()) != null) {
// Split the line into individual parts, on the <tab> character
String parts[] = line.split("\t");
int sum = 0;
// Staring from the first number, sum the line...
for (int index = 1; index < parts.length; index++) {
sum += Integer.parseInt(parts[index].trim());
}
// Store the key/value pairs together some how
}
}
現在,我們需要一些方法來存儲計算的結果...
看一看Basic I/O瞭解詳情
管理數據
現在,有任意數量的,你可以做到這一點的方式,但由於數據量是可變的,你想有一個數據結構,可以動態增長。
我首先想到的是使用Map
,但這種假設要行具有相同名稱的結合,否則你應該只是一個List
,其中外List
代表行和Inner
列表代表中我們List
列值...
Map<String, Integer> data = new HashMap<>(25);
File[] files = listFiles(someDir, januraryFilter);
for (File file : files {
readFile(file, data);
}
哪裏readFile
基本上是從代碼之前
protected void readData(File file, Map<String, Integer> data) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line = null;
// Read each line
while ((line = br.readLine()) != null) {
//...
// Store the key/value pairs together some how
String name = parts[0];
if (data.containsKey(name)) {
int previous = data.get(name);
sum += previous;
}
data.put(name, sum);
}
}
}
看一看的Collections Trail更多細節
顯示數據
最後,我們需要顯示的數據。你可以簡單地用一個DefaultTableModel
,但你已經在結構中的數據,爲什麼不使用自定義TableModel
public class SummaryTableModel extends AbstractTableModel {
private Map<String, Integer> data;
private List<String> keyMap;
public SummaryTableModel(Map<String, Integer> data) {
this.data = new HashMap<>(data);
keyMap = new ArrayList<>(data.keySet());
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
Class type = Object.class;
switch (columnIndex) {
case 0:
type = String.class;
break;
case 1:
type = Integer.class;
break;
}
return type;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
switch (columnIndex) {
case 0:
value = keyMap.get(rowIndex);
break;
case 1:
String key = keyMap.get(rowIndex);
value = data.get(key);
break;
}
return value;
}
}
重新使用它,那麼只需在它應用到JTable
...
add(new JScrollPane(new JTable(new SummaryTableModel(data)));
看看How to Use Tables更多細節
結論
有很多是必須作出的假設這是從問題的情況下丟失的;文件的順序是否重要?你關心重複的條目嗎?
使它成爲幾乎不可能提供一個單一的「答案」,這將解決所有你的問題
*「如果有人知道任何解決方案」 *編寫代碼。 *「..或者可以鏈接任何類似的計算..」* OT爲SO。 –
解決問題。你需要閱讀文件。首先閱讀一個,查看[Basic I/O](http://docs.oracle.com/javase/tutorial/essential/io/)瞭解更多詳情。一旦你明白了這一點,你需要弄清楚如何列出滿足所需模式的文件,如['java.io.File'](https://docs.oracle.com/javase/7/docs /api/java/io/File.html)或[文件I/O(具有NIO.2)](https://docs.oracle.com/javase/tutorial/essential/io/fileio.html)可以做到。然後你需要決定如何管理數據,但是沒有足夠的上下文來回答這個問題,但是'List'的'Map'可以幫助 – MadProgrammer
最後,你需要把所有的東西都放到'JTable'中,看看[如何使用表格](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)瞭解更多詳情。所以,真的,你問了四個問題...... – MadProgrammer