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();
        }
    }
}
Ram
  • 3,092
  • 10
  • 40
  • 56
wadefanyaoxia
  • 597
  • 1
  • 8
  • 21
  • 5
    Why not use one of [the available CSV libraries](http://stackoverflow.com/questions/101100/csv-api-for-java)? – assylias Jul 24 '13 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? – Martin Jul 24 '13 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 – AlexWien Jul 24 '13 at 14:39
  • did you close the file? writer.close(); ? If yes then the problemn is not csv related, your contenrt is simply wromg. (date formatter, etc.) – AlexWien Jul 24 '13 at 14:41
  • 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. – assylias Jul 24 '13 at 14:41
  • @AlexWien Maybe (just maybe) he is doing that in the **unpublished** `finally` block that should come after the `try`. Anyway, it is likely that the stream is never being flushed. – LexLythius Jul 24 '13 at 14:41
  • @assylias, yes but this is the only pitfall. if you forget that or use "," as delimter and "," as decimal symbols. – AlexWien Jul 24 '13 at 14:49
  • 1
    @AlexWien I'm just saying that adding `net.sf.opencsvopencsv` 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. – assylias Jul 24 '13 at 14:55
  • @AlexWien Yes I have used writer.close(), I have added the entire class code. And for the time format, I can successfully print 2013/07/22 15:40:23. But in the csv file, only shown 2013/07/22 15:40. There is no 23. – wadefanyaoxia Jul 24 '13 at 15:05
  • Are you opening the CSV file in a text editor or a spreadsheet? If the latter, have you tried resizing the column? – Andy Thomas Jul 24 '13 at 15:09
  • @AndyThomas Thanks for reply, I am open it with excel but have not resize the column, is that really important to resize columns? – wadefanyaoxia Jul 24 '13 at 15:16
  • If the column were too small to show the whole time, it could result in the seconds not being visible in the output. It would be worthwhile to double-check in a text editor that the seconds are not in the output at all, rather than just not appearing in the spreadsheet presentation. – Andy Thomas Jul 24 '13 at 15:20
  • Open the history.csv in notepad.exe or wordpad.exe and post the first lines here – AlexWien Jul 24 '13 at 16:46
  • Tip: always close streams in a finally clause. See [this](http://www.javapractices.com/topic/TopicAction.do?Id=8) and [this](http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html) for Java 7. – LexLythius Jul 24 '13 at 16:49
  • @AlexWien Title,2013/07/24 18:17:42,2013/07/24 18:18:00. It shows the seconds....I am using mac and use the TextEdit to open it. – wadefanyaoxia Jul 24 '13 at 17:21

1 Answers1

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.]

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151