Je passe dans une imprimante CSVPrinter dans une méthode et en interne dans une boucle j'appelle la méthode printRecord. Voici l'extrait de la méthode.CSVPrinter printRecord java.io.IOException: Stream fermé
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();
}
}
}
L'imprimante est en cours d'initialisation par une autre méthode dans la principale méthode de la façon suivante:
public void Main(){
.....
CSVFormat format = getformatWithHeaders();
final CSVPrinter printer = getCSVPrinter(format);
.....
for(String string: Strings){
.......
printToCsv(currentDate, contrs, printer);
.......
}
}
Snippet de getCSVPrinter (format) Méthode:
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;
}
Les impressions en-tête à le fichier de sortie, mais chaque fois qu'un enregistrement essaie d'être imprimé, je reçois java.io.IOException: Stream closed
. La manipulation des OI a toujours été un cauchemar pour moi. S'il vous plaît aider!
Le CSVPrinter est de org.apache.commons.csv
@sophist_pt J'ai ajouté un exemple qui utilise une expression de lamda pour répondre à vos besoins. Avoir un essai ... –