Creating a CSV file using java

I am going to create CSV files using java. Here is the piece of code:

try{ FileWriter writer = new FileWriter(sFileName); writer.append("Title"); for(StoredArticle sa3:historyFile.keySet()){ for(String k3:sa3.getTimeAndPopularity().keySet()){ writer.append(','); writer.append(k3); } } writer.append('\n'); 

The problem is that I am successfully creating a CSV file. And in the for k3 cycle, this is the time represented as the format 2013/07/22 15:40:23. But the seconds "23" cannot be shown. The rest show good results. what problem please help.

This is the code of my whole class.

 package uk.ac.ncl.fanyaoxia.createCSV; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; import uk.ac.ncl.fanyaoxia.monitor.MonitorRecentUpdates; import uk.ac.ncl.fanyaoxia.monitor.StoredArticle; import uk.ac.ncl.fanyaoxia.webpagefetch.ReadXml; public class CreateCSVFile { private static Map < StoredArticle, ReadXml > historyFile; public CreateCSVFile() { historyFile = new HashMap < StoredArticle, ReadXml > (); } public void createFile() { generateCsvFile("HistoryTable.csv"); } private static void generateCsvFile(String sFileName) { MonitorRecentUpdates csvFile = new MonitorRecentUpdates(); historyFile = csvFile.getMap(); try { FileWriter writer = new FileWriter(sFileName); writer.append("Title"); for (StoredArticle sa3: historyFile.keySet()) { for (String k3: sa3.getTimeAndPopularity().keySet()) { writer.append(','); writer.append(k3); } } writer.append('\n'); for (StoredArticle sa3: historyFile.keySet()) { writer.append(sa3.getStoredTitle()); for (String k3: sa3.getTimeAndPopularity().keySet()) { writer.append(','); writer.append(sa3.getTimeAndPopularity().get(k3).toString()); } writer.append('\n'); } writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } 
+4
source share
1 answer

Seconds are output as expected by code. They are visible in a text editor.

They simply were not visible in the MS Excel spreadsheet application. One possible reason would be that the column width was too small.

[This answer summarizes the result of the conversation between the OP and me above.]

+3
source

Source: https://habr.com/ru/post/1493223/


All Articles