我通过一种方法传递CSVPrinter打印机,并且在内部循环中传递了printRecord方法。 下面是该方法的代码段。
private void printToCsv(String currentDate, LinkedHashMap> contracts, CSVPrinter printer){
List activeContracts = ContractMonthlyStatsReader.getListOfActiveContracts(currentDate, contrs);
for (String activeContract: activeContracts){
CMonthlyStatsR report = getCMonthlyStatsR(currentDate, activeContr, contracts.get(activeContr));
try {
printer.printRecord(
report.getAID(),
report.getMD(),
report.getAL(), report.getEL(),
ReportUtil.getMatchValue(),
report.getAFL(), report.getEFL(),
ReportUtil.getMatchValue(),
report.getAPF(), report.getEPF(),
ReportUtil.getMatchValue()
);
printer.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
正在通过main方法中的另一种方法通过以下方式初始化打印机:
public void Main(){
.....
CSVFormat format = getformatWithHeaders();
final CSVPrinter printer = getCSVPrinter(format);
.....
for(String string: Strings){
.......
printToCsv(currentDate, contrs, printer);
.......
}
}
getCSVPrinter(format)方法的代码段:
private CSVPrinter getCSVPrinter(CSVFormat format){
try (final FileWriter newWriter = new FileWriter(System.getProperty("output.file"))){
try(final CSVPrinter printer = new CSVPrinter(newWriter, format)){
return printer;
}
}
catch (IOException e){
e.printStackTrace();
}
return null;
}
标题打印到输出文件,但是每次尝试打印记录时,我都会得到java.io.IOException: Stream closed。 处理IO一直是我的噩梦。 请帮忙!
CSVPrinter来自org.apache.commons.csv
您遇到的错误是Try-with-resources-statement的错误解释。
在您的try块中,您先打开FileWriter,然后打开CSVPrinter:
try (final FileWriter newWriter = new FileWriter(System.getProperty("output.file"))){
try(final CSVPrinter printer = new CSVPrinter(newWriter, format)){
return printer;
}
}
catch (IOException e){
e.printStackTrace();
}
但是当您退回打印机时,它已经关闭,因为try-with-resources语句已关闭。
因此,如果您想退回打印机,请不要尝试使用资源
请查看此问题以获取更多信息:https://stackoverflow.com/a/22947904/5515060
我完全可以做到。 但是我将如何确保流关闭。 因为在主要范围内无法访问作者。 我在想也许我可以尝试在main方法范围的finally块中关闭打印机。 这样可以确保正确关闭流吗?
@sophist_pt最简单的方法是删除整个getCSVPrinter()方法,然后将代码粘贴到使用它的位置。 否则您会遇到很多工作,这似乎是不必要的
您可以在getCSVPrinter方法中使用try-with-resources功能。 如果离开此块,它将自动关闭流。
try(final CSVPrinter printer = new CSVPrinter(newWriter, format)){
return printer;
}
一种解决方案是删除try块,然后仅返回new CSVPrinter(newWriter, format)。 但是,随后您必须自己关闭操作流。
另一个解决方案是在try-with-resources块内进行所有流处理。
这是一个使用使用者接口在try-with-resources块中写入CsvPrinter的示例。
public void Main(){
.....
CSVFormat format = getformatWithHeaders();
.....
for(String string: Strings){
.......
printToCsv(currentDate, contrs, format);
.......
}
}
private void printToCsv(String currentDate, LinkedHashMap> contracts, CSVFormat format){
List activeContracts = ContractMonthlyStatsReader.getListOfActiveContracts(currentDate, contrs);
for (String activeContract: activeContracts){
CMonthlyStatsR report = getCMonthlyStatsR(currentDate, activeContr, contracts.get(activeContr));
printToCSV(format, printer -> {
try {
printer.printRecord(
report.getAID(),
report.getMD(),
report.getAL(), report.getEL(),
ReportUtil.getMatchValue(),
report.getAFL(), report.getEFL(),
ReportUtil.getMatchValue(),
report.getAPF(), report.getEPF(),
ReportUtil.getMatchValue()
);
printer.flush();
}
catch (IOException e) {
e.printStackTrace();
}
});
}
}
private void printToCSV(CSVFormat format, Consumer consumer){
try (final FileWriter newWriter = new FileWriter(System.getProperty("output.file"))){
try(final CSVPrinter printer = new CSVPrinter(newWriter, format)){
consumer.accept(printer);
}
}
catch (IOException e){
e.printStackTrace();
}
}
@sophist_pt Ive添加了一个示例,该示例使用lamda表达式来满足您的需求。 试试...