메서드에서 CSVPrinter 프린터를 전달할 때 내부적으로 루프에서 printRecord 메서드를 호출하고 있습니다. 아래는 메소드의 스 니펫입니다.CSVPrinter printRecord java.io.IOException : 스트림이 닫혔습니다
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();
}
}
}
프린터는 다음과 같은 방법으로 주된 방법으로 다른 방법에 의해 초기화되고 :
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
@sophist_pt : 프린터를 반환 할 경우
그래서, 다음 자원
으로 시도 자세한 내용은이 질문에서보세요 사용하지 말아 귀하의 필요에 맞게. 시도해보십시오 ... –