我使用的是 opencsv 3.0 版本。由于某种原因,它没有将记录写出到 CSV 文件中。它适用于 2.3 版。但是,我想使用 CSVIterator 类。感谢您的帮助。
public void writeGradesIntoFile(GradeReport gradeReport,String outputFileName)
{
File outputCSVFile = null;
CSVWriter csvFileWriter = null;
String[] lastRecord = null;
try {
outputCSVFile = new File(outputFileName + ".csv");
csvFileWriter = new CSVWriter(new FileWriter(outputCSVFile, true),
',', CSVWriter.NO_QUOTE_CHARACTER);
if (checkFileExistsAndEmpty(outputCSVFile)) {
String[] firstRecord = new String[] { gradeReport.getEmpId(),
Integer.toString(gradeReport.getScore()) };
csvFileWriter.writeNext(firstRecord);
}
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
}
这是pom入口
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.0</version>
</dependency>
最佳答案
您需要通过调用 csvFileWriter.close()
关闭编写器 - 理想情况下,这应该在 finally
block 中完成。
关于java - opencsv 不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27095363/