private Runnable dodownload() throws IOException {
return new Runnable() {
@Override
public void run() {
String basepath = getResourceBasePath();
List tfeepaids = getTfeepaidByTime();
String timeName = getOneDayForDown();
String KjDownResourcePath = "";
String id = "";
for (Tfeepaid tfeepaid : tfeepaids) {
id = tfeepaid.getFcertid();
KjDownResourcePath = new File(basepath, "down/" + id + "-" + timeName + ".txt").getAbsolutePath();
ensureDirectory(KjDownResourcePath);
StringBuffer sb = new StringBuffer();
sb.append(tfeepaid.getFlastcommit());
sb.append("\t");
sb.append("\r\n");
// TODO 其他数据据字段的设置20190314
try {
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(KjDownResourcePath)));
bw.write(sb.toString());
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
}
从代码得知,取出来的集合
List tfeepaids = getTfeepaidByTime();
是全部用户数据的话,直接遍历,因为KjDownResourcePath的路径相同,所以后面的一定会将前面的内容覆盖掉。
所以可以将同一个用户的数据,放在list集合中,然后使用用户id当key,放于map中。
然后再通过key,遍历map,一次性取出用户的集合数据,输出到文档。
private Runnable dodownload() throws IOException {
return new Runnable() {
public void run() {
String basepath = getResourceBasePath();
List<Tfeepaid> tfeepaids = getTfeepaidByTime();
String timeName = getOneDayForDown();
String KjDownResourcePath = "";
// 将同一个用户的数据,放在list集合中,然后使用用户id当key,放于map中。
Map<String, List<Tfeepaid>> tfeepaidMap = new HashMap<String, List<Tfeepaid>>();
for (Tfeepaid tfeepaid : tfeepaids) {
List<Tfeepaid> tfeepaidList = tfeepaidMap.get(tfeepaid.getFcertid());
if (tfeepaidList == null) {
tfeepaidList = new ArrayList<Tfeepaid>();
}
tfeepaidList.add(tfeepaid);
tfeepaidMap.put(tfeepaid.getFcertid(), tfeepaidList);
}
Set<String> keys = tfeepaidMap.keySet();
// 通过key,遍历map,取出集合数据,输出到文档。
for (String key : keys) {
List<Tfeepaid> tfeepaidList = tfeepaidMap.get(key);
KjDownResourcePath = new File(basepath, "down/" + key + "-" + timeName + ".txt").getAbsolutePath();
ensureDirectory(KjDownResourcePath);
StringBuffer sb = new StringBuffer();
// 输出数据为Flastcommit,也可以拼接其他数据。
for (Tfeepaid tfeepaid : tfeepaidList) {
sb.append(tfeepaid.getFlastcommit());
sb.append("\r\n");
}
try {
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(KjDownResourcePath)));
bw.write(sb.toString());
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
}