java - 数据未使用 Apache Commons 写入 csv

标签 java csv javafx apache-commons

我正在尝试使用 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());
   }

最佳答案

为什么需要几十行代码和第三方库来完成 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,但这是另一个问题。

关于java - 数据未使用 Apache Commons 写入 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44973123/

相关文章:

javascript - 将 CSV 转换为带有 header 的 JSON

java - 获取所选元素在 JList 中的位置

java - 我怎样才能知道线程中断后还剩下多少时间可以 hibernate ?

python - 如何使用 pandas.read_csv 将 CSV 文件中的数据插入到数据框中?

asp.net - 正则表达式解析 csv

javafx - 使用 Scene builder 在 JavaFX 中制作一个简单的 TreeView

java - 错误 : Could not find or load main class com. mycompany.bazybazy.MainApp - 使用 JavaFX 的 Maven 项目

user-interface - 如何在 JavaFX 场景中定位按钮(或任何 GUI 元素)?

java - Grails:Linux 上的 Jasper 插件错误 - java.lang.reflect.UndeclaredThrowableException

java - 复选框和相应行的值从 jsp 到 java Controller 文件