获取空CSV文件

获取空CSV文件,csv,groovy,concurrenthashmap,Csv,Groovy,Concurrenthashmap,我试图将hashmap数据写入CSV文件,但我看到生成了空文件。以下是我正在使用的代码: String fileName for (String key : concurrentHashMap.keySet()) { logInfo(key + ": ")//gives the name of the file logInfo("concurrentHashMapKey" + concurrentHashMap.get(key)) file

我试图将hashmap数据写入CSV文件,但我看到生成了空文件。以下是我正在使用的代码:

String fileName
    for (String key : concurrentHashMap.keySet()) {
        logInfo(key + ": ")//gives the name of the file
        logInfo("concurrentHashMapKey" + concurrentHashMap.get(key))
        fileName = key
        logInfo("fileName: " + fileName)
        helpers.writeHashMapToCsv(concurrentHashMap, fileName)
    }

public void writeHashMapToCsv(ConcurrentHashMap<String, String> collectedCSV, String fileName) throws Exception {
        String eol = System.getProperty("line.separator")
        try{
            Writer writer = new FileWriter(".\\baseline-files\\" + fileName)
            for (Map.Entry<String, String> entry : collectedCSV.entrySet()) {
                println "entry = $entry"
                writer.append(entry.getKey())
                        .append(',')
                        .append(entry.getValue())
                        .append(eol)
            }
        } catch (IOException ex ) {
            ex.printStackTrace(System.err)
        }
    }
字符串文件名
for(字符串键:concurrentHashMap.keySet()){
logInfo(key+“:”)//提供文件名
logInfo(“concurrentHashMapKey”+concurrentHashMap.get(key))
文件名=键
logInfo(“文件名:”+fileName)
writeHashMapToCsv(concurrentHashMap,文件名)
}
public void writeHashMapToCsv(ConcurrentHashMapCollectedCSV,字符串文件名)引发异常{
字符串eol=System.getProperty(“line.separator”)
试一试{
Writer Writer=newfilewriter(“.\\baseline files\\”+文件名)
对于(Map.Entry:collectedCSV.entrySet()){
println“entry=$entry”
writer.append(entry.getKey())
.append(“,”)
.append(entry.getValue())
.append(下线)
}
}捕获(IOEX异常){
例如printStackTrace(System.err)
}
}
有人能帮帮我吗?我做错了什么

注: concurrentHashMap大小=2

concurrentHashMap=[NetworkLeaxing\u Main\u crosstab.csv:%的总付款额不支持服务网络,网站类型总付款额不支持服务网络 网络外家庭健康1%$247870 家庭健康拥有6%的股份1306259美元 网络外的熟练护理设施4%$860545 熟练护理机构拥有18%的资金3919193美元 ,IP_RehabSNF_Profiles_crosstab.csv:设施站点类型网络从属关系剧集数总付款占总付款的百分比平均剧集付款ALOS CMS评级 LORRETTA HOSPITAL Anchor拥有28美元344392%12300美元3
LORRETTA HOSPITAL Home Health Out Of Network 87$14358 0%$2872
LORRETTA HOSPITAL Home Health拥有19$2981 0%$2981
洛雷塔医院网络外门诊77$3706 0%$71
洛雷塔医院门诊拥有1593美元149194 1%288美元
]

因此,这里有两组键(文件名)和值(数据),我需要为它们生成以下两个文件: 1.网络泄漏\u主\u交叉表.csv

  • IP_RehabSNF_Profiles_crosstab.csv
  • 添加
    writer.close()
    作为
    writer
    缓冲数据

    最好将其添加到
    finally
    块:

    } catch (IOException ex) {
        ex.printStackTrace(System.err)
    } finally {
        writer.close();
    }
    

    更改更简单:

            Writer writer = new FileWriter(".\\baseline-files\\" + fileName)
            for (Map.Entry<String, String> entry : collectedCSV.entrySet()) {
                println "entry = $entry"
                writer.append(entry.getKey())
                        .append(',')
                        .append(entry.getValue())
                        .append(eol)
            }
    

    这将自动关闭writer

    如果您使用的是groovy,那么它可能会重复,groovy是处理csv的最简单方法-
            new File("./baseline-files/$fileName").withWriter { writer ->
                collectedCSV.each { key, value ->
                    writer.writeLine "$key,$value"
                }
            }