【问题标题】:data not being written to csv with Apache Commons未使用 Apache Commons 将数据写入 csv
【发布时间】:2017-12-11 21:22:15
【问题描述】:

我正在尝试使用 apache commons 运行以下代码,以便将其写入 csv 文件。 finalResult 数组的打印语句表明它包含我想要填充的内容。由于某种原因,它只是不将内部的数据发送到指定的 csv。

public void handle(ActionEvent runButton) {
    String csvFile = (userHomeFolder + "/" + fileName.getText() + ".csv");
    FileWriter writer = new FileWriter(csvFile);
    try {

        final Object [] FILE_HEADER = columnHeaders.toArray();
        int modval = 2;
        final String NEW_LINE_SEPARATOR = "\n";
        FileWriter fileWriter;
        CSVPrinter csvFilePrinter;
        CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
        List rowResult = new ArrayList();

        fileWriter = new FileWriter(fileName.getText());
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

        try {

            //CREATE CSV FILE HEADER
            System.out.println("File header: " + FILE_HEADER);
            csvFilePrinter.printRecord(FILE_HEADER);

            for(int y = 0; y < finalResult.size(); y++) {

                if(y % modval == 0) {
                    rowResult.add(finalResult.get(y));
                    csvFilePrinter.printRecord(rowResult);
                    System.out.println(rowResult);
                    rowResult.clear();
                }else {
                    //this means it is not the last value in the row
                    rowResult.add(finalResult.get(y));
                    System.out.println("fr: " + finalResult.get(y));
                }

            }

        } catch (Exception e) {
            System.out.println("Error in csvFileWriter");
            e.printStackTrace();

        } finally {
            try {
                fileWriter.flush();
                fileWriter.close();
                csvFilePrinter.close();
            } catch (IOException e ) {
                System.out.println("Error while flushing/closing");
            }
        }
    }
}
    //below catches any errors for any of the above work
    catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
   }

【问题讨论】:

  • 您使用传统的try 没有catchfinally 或资源声明,这就是它无法编译的原因!此外,最好在 wirter 使用它们之后关闭底层写入器,因为您不知道它们是否使用缓冲......
  • 能否将finalResult的内容添加到问题中。同样,当您执行两个打印输出语句时,您会看到什么?
  • 为什么你有两个FileWriters?似乎您只写过其中一个 - 不是带有“csv”后缀的那个?
  • @sillyfly csvFilePrinter 使用 FileWriter
  • 有一个FileWriterwriter,还有一个叫fileWriter。名为writer 的那个在创建后从未使用过,它实际上是用 .csv 后缀初始化的。

标签: java csv javafx apache-commons


【解决方案1】:

为什么你需要几十行代码和一个第三方库来完成 printf 可能只需要 5 行就能完成的事情?创建PrintWriter 并使用printf 写入。完成。

try (PrintWriter pw = new PrintWriter(csvFile)) {
    pw.printf("%s\n", FILE_HEADER);

    for(int i = 0; i < finalResult.size(); i+=2) { // no need to modulo
        pw.printf("%s\n", finalResult.get(i)); // no idea what type finalResult.get(i) is, format it if you need to
    }
}

您不应该通过硬编码文件分隔符/ 来创建csvFile,但这是另一个问题。

【讨论】:

  • 复杂代码的原因是它需要根据用户输入处理未知数量的列。然后我希望它写入一个 csv 并在每次索引达到列标题数组的大小时创建一个新行,以便它被整齐地格式化。我只是简化了代码以更快地找到主要问题解决方案
  • 您可以在上面的代码中完成所有这些操作。您不需要知道列数,只需在 , 后面加上任何内容并打印即可。
猜你喜欢
  • 1970-01-01
  • 2019-08-06
  • 2019-05-07
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 2016-10-15
相关资源
最近更新 更多