我正在一个方法中传入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(格式)方法的片段:
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
答案 0 :(得分:1)
您可以在getCSVPrinter
- 方法中使用try-with-resources功能。如果你离开这个区块,它会自动关闭流。
try(final CSVPrinter printer = new CSVPrinter(newWriter, format)){
return printer;
}
一种解决方案是删除try-block并返回new CSVPrinter(newWriter, format)
。但是你必须自己关闭你的操作流。
另一个解决方案是在try-with-resources-block中进行所有流处理。
这是一个使用使用者界面在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();
}
}
答案 1 :(得分:1)
您遇到的错误是对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-statement已关闭。
因此,如果您想要退回打印机,请不要尝试使用资源
有关详细信息,请查看此问题:https://stackoverflow.com/a/22947904/5515060