5

I am going to create CSV files by using java. Here is a part of the 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 I am successfully create the CSV file. And in the for loop k3 is the time presented as format 2013/07/22 15:40:23. But the seconds "23" cannot be shown. The others are showing good. what's the problem please help.

This is the code of my entire 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();
        }
    }
}
15
  • 5
    Why not use one of the available CSV libraries? Commented Jul 24, 2013 at 14:33
  • 1
    First you would have to check (or at least provide) the contents of your collections. If you print the time to the console, does it show properly? Commented Jul 24, 2013 at 14:36
  • 2
    @assylias because csv are so simple that there is no need to have an dependency to an externaL lib where you have to check the license condition. Wrting a "," and a new line, that is all you need in CSV Commented Jul 24, 2013 at 14:39
  • 2
    @AlexWien Not that simple. For example your data might contain strings with commas inside, which you need to include in quotes etc. In the end it is not very complicated, but I don't see any issues in importing a small dependency to handle that for me so that I can move on. Commented Jul 24, 2013 at 14:41
  • 1
    @AlexWien I'm just saying that adding <dependency><groupId>net.sf.opencsv</groupId><artifactId>opencsv</artifactId></dependency> to my maven project is quicker and safer. And at some stage you will probably want to parse the files too, which makes dealing with escaping quotes a little more difficult. And before you realise, you have 400 more lines to maintain. But that is an endless discussion. Commented Jul 24, 2013 at 14:55

1 Answer 1

3

The seconds are being output as expected by the code. They are visible in a text editor.

They just weren't visible in the spreadsheet application MS Excel. One possible cause would be that the column width was too small.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.