weixin_43490664 2019-03-13 20:32 采纳率: 100%
浏览 312
已采纳

为什么我的循环只走一次,只导出同一天同一个的第一次查询的记录,应该是导出同一天同一个人的所有记录

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();
}

            }

        }
    };
}



  • 写回答

1条回答 默认 最新

  • TinerSky 2019-03-13 21:17
    关注

    从代码得知,取出来的集合

    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();
                        }
                    }
                }
            };
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?