views:

195

answers:

4

I am creating and writing to a file using the following pattern:

File afile = new File("C:/dev/ws/DataOrdering/data/" + thisDate
    + "_" + thisTime + "_visdata.csv");
FileWriter writer = new FileWriter(afile);
writer.append(tradeDetails);
writer.close();

However for some reason only the first file is written to, after that the files are created by they are empty - they would only be created if there was a record at the particular time, as the file names are based on times taken from the records. My complete method is printed below. (I have edited it to reflect the changes I made).

public void createTimeFiles() throws IOException {

 CSVReader reader = new CSVReader(new FileReader(
   "C:/dev/ws/DataOrdering/data/visdata.csv"));

 String[] nextLine;
 String lastTime = "";
 String code, date, hour, min, sec, offset, type, price, volume, bid, ask, headline;

 HashMap<Integer, FileWriter> writers = new HashMap<Integer, FileWriter>();
 while ((nextLine = reader.readNext()) != null) {
  String thisDate = nextLine[1];
  String thisTime = nextLine[2].substring(0, 5);

  code = nextLine[0];
  date = nextLine[1];
  hour = nextLine[2].substring(0, 2);
  min = nextLine[2].substring(3, 5);
  sec = nextLine[2].substring(6);
  offset = nextLine[3];
  type = nextLine[4];
  price = nextLine[5];
  volume = nextLine[6];
  bid = nextLine[7];
  ask = nextLine[7];
  headline = nextLine[7];

  // System.out.println(thisDate + " - " + thisTime + " - " + hour
  // + " - " + min);
  String tradeDetails = code + " _ " + date + " _ " + hour + " _ "
    + min + " _ " + sec + " _ " + offset + " _ " + type + " _ "
    + price + " _ " + volume + " _ " + bid + " _ " + ask
    + " _ " + headline;

  File afile = new File("C:/dev/ws/DataOrdering/data/" + thisDate
    + "_" + thisTime + "_visdata.csv");
  if (afile.exists()) {
   FileWriter writer = new FileWriter(afile);
   writer.append(tradeDetails);
   writer.close();
  } else {
   System.out.println("the file exists");
   FileWriter writer = new FileWriter(afile);
   writer.write(tradeDetails);
   writer.close();
  }

 }
}
A: 

Maybe visdata.csv doesn't exist or contains no data, therefore nothing would be written.

Does C:/dev/ws/DataOrdering/data/visdata.csv exist?

Steven
Thats a good suggestion, but I've checked it, its not the problem.
Ankur
+2  A: 

Does the file exist? You're appending, not writing....

Mark Mayo
+1  A: 

Check your "!"

            if (!afile.exists()) { // here
                    System.out.println("the file exists");
                    FileWriter writer = new FileWriter(afile);
                    writer.append(tradeDetails);
                    writer.close();
            } else {
                    FileWriter writer = new FileWriter(afile);
                    writer.append(tradeDetails);
                    writer.close();
            }

Why do you do two times the same?? If the file doesn't exists. You have to write, not append.

It is also possible that the folder not exists.

aFile.getParentFile().mkdirs();

If the parent-folder already exists, there is no problem.

Martijn Courteaux
@Martijin, thanks I changed it to be more sensible.
Ankur
A: 

Call

writer.flush();

before calling

writer.close();
Pierre
@Pierre, I was under the impression that close() calls flush(). (and reading through the Java 5 source seems to confirm this)
Glen
@ Glen, yes I thought so too, I tried it for arguments sake. Same issue remains.
Ankur
Oh, yes, sorry. I was confused with OutputStream
Pierre