I need to create a single string named CSVOutput, and load all keys and values from the ConcurrentHashMap<String, Integer>
, counts into this file separated by commas.
So I'll have two columns in the resulting csv file, one is for the keys and the other one for the values.
I have written this code for my method OutputCountsAsCSV:
private void OutputCountsAsCSV(ConcurrentHashMap<String, Integer> counts, String filename) {
String CSVOutput = new String("");
for (Entry<String, Integer> entry : counts.entrySet()) {
String rowText = String.format("%s,%d\n", entry.getKey(), entry.getValue()); # defines new lines/rows
CSVOutput = rowText;
try (FileWriter writer = new FileWriter(filename)) {
writer.write(CSVOutput);
}
catch (Exception e) {
System.out.println("Saving CSV to file failed...");
}
}
System.out.println("CSV File saved successfully...");
}
However, I'm told I should have followed this order:
- create and prepare file
- for each entry, write data to file
- close the file (and print that it happened successfully).
Indeed, my System.out.println("CSV File saved successfully...")
seems to be inside the loop and is being printed many times.
How can I do it correctly?
I am a beginner in Java, so I'm very grateful for any help.
CodePudding user response:
Here is a better approach (do your own research about the finally keywords)
private void OutputCountsAsCSV(ConcurrentHashMap<String, Integer> counts, String filename) {
try (FileWriter writer = new FileWriter(filename)) {
for (Entry<String, Integer> entry : counts.entrySet()) {
String rowText = String.format("%s,%d\n", entry.getKey(), entry.getValue());
writer.append(rowText);
}
} catch (Exception e) {
System.out.println("Saving CSV to file failed...");
} finally {
System.out.println("CSV File saved successfully...");
}
}
CodePudding user response:
Note to other readers: We used try-with-resources syntax to make sure the file is always closed gracefully, even when encountering exceptions. The code in the Question does the same. See The Java Tutorials by Oracle Corp.
This Question & Answer is for learning purposes. In real work, I would not generate CSV text. I would use one of the several good libraries available in the Java ecosystem for handling CSV text. Example: Apache Commons CSV.