我正在以方法传入CSVPrinter打印机,并在内部循环中调用printRecord方法。下面是该方法的代码片段。
private void printToCsv(String currentDate, LinkedHashMap<String, LinkedList<CMonthlyStats>> contracts, CSVPrinter printer){
List<String> 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
您可以在getCSVPrinter
方法中使用"试用资源"功能。如果您离开此块,它会自动关闭流。
try(final CSVPrinter printer = new CSVPrinter(newWriter, format)){
return printer;
}
一种解决方案是删除 try-block 并返回 new CSVPrinter(newWriter, format)
.但是,您必须自己关闭操作后的流。
另一种解决方案是在资源块中执行所有流处理。
下面是一个示例,它使用使用者接口在 try-with-resources-block 中写入 CsvPrinter。
public void Main(){
.....
CSVFormat format = getformatWithHeaders();
.....
for(String string: Strings){
.......
printToCsv(currentDate, contrs, format);
.......
}
}
private void printToCsv(String currentDate, LinkedHashMap<String, LinkedList<CMonthlyStats>> contracts, CSVFormat format){
List<String> 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<CSVPrinter> 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();
}
}
您遇到的错误是对Try-with-resources-statement
的错误解释。
在您的尝试块中,您打开了一个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