Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

write() without immediate flush() losing data...  RSS feed

 
John Kramer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I'm missing something subtle (hmmm... hopefully it's obvious...) in writing output to multiple files. The code below works --- but only after adding the .flush() method after the write() methods.

Using the .flush() prior to closing the files did not work. Without the .flush() after the write only two of the four files would contain the header text after the program ran (there is no data output at this stage in the code development).

For clarity, I'll say this differently: without the "write() then immediate flush()" sequence only two of the four output files contain any text at all.

While the code works, 1) I don't like having to flush after every write, and 2) I really want to understand what's happening here. Actually, all other constructive criticism is also welcomed.

Thanks!
John

(Dev Environment: XP home, Eclipse, j2sdk1.4.2_04 )

------------------------------------------------
package com.acme.bdt.parser.reader.processor.csvoutput;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

/**
* @author johnk
*
*/
public class CsvReportWriter {

private static CsvReportWriter theInstance = null;

protected static String baseDir = "c:/var/SupraData/Parsed/";

protected static final String occBaseSummaryFilename = "OCC_Summary.csv";
protected static final String occBaseDetailFilename = "OCC_Detail.csv";
protected static final String CsrBaseSummaryFilename = "CSR_Summary.csv";
protected static final String CsrBaseDetailFilename = "CSR_Detail.csv";
protected static final String zipFileBaseName = "parsed.zip";

protected static String occSummaryFname = null;
protected static String mrcSummaryFname = null;
protected static String occDetailFname = null;
protected static String mrcDetailFname = null;
protected static String zipFileName = null;

protected static String billDate = "";
protected static String ban = "";

protected File occSummaryFile = null;
protected File occDetailFile = null;
protected File mrcSummaryFile = null;
protected File mrcDetailFile = null;

protected Writer occSummaryOut = null;
protected Writer occDetailOut = null;
protected Writer mrcSummaryOut = null;
protected Writer mrcDetailOut = null;

private boolean openedFiles;

// Create Log4j category instance for logging
static private org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(CsvReportWriter.class.getName());


private CsvReportWriter() {}

static {
theInstance = new CsvReportWriter();
}

public static CsvReportWriter getInstance() { return theInstance; }

public boolean isOpen() { return openedFiles; }

public void open( String ban, String bdate ) throws IOException
{
String filenamePrefix = ban + "_" + bdate + "_";

occSummaryFname = baseDir + filenamePrefix + occBaseSummaryFilename;
mrcSummaryFname = baseDir + filenamePrefix + CsrBaseSummaryFilename;
occDetailFname = baseDir + filenamePrefix + occBaseDetailFilename;
mrcDetailFname = baseDir + filenamePrefix + CsrBaseDetailFilename;
zipFileName = baseDir + filenamePrefix + zipFileBaseName;

occSummaryFile = new File(occSummaryFname);
occDetailFile = new File(occDetailFname);
mrcSummaryFile = new File(mrcSummaryFname);
mrcDetailFile = new File(mrcDetailFname);

occSummaryOut = new PrintWriter( new FileWriter(occSummaryFile) );
occDetailOut = new PrintWriter( new FileWriter(occDetailFile) );
mrcSummaryOut = new PrintWriter( new FileWriter(mrcSummaryFile) );
mrcDetailOut = new PrintWriter( new FileWriter(mrcDetailFile) );

log.info("MRC Summary File Name=<" + mrcSummaryFname + ">");
log.info("MRC Detail File Name=<" + mrcDetailFname + ">");
log.info("OCC Summary File Name=<" + occSummaryFname + ">");
log.info("OCC Detail File Name=<" + occDetailFname + ">");

String header = OccSummaryCsvRecord.csvHeaderString();
log.debug( header );
occSummaryOut.write( header );
occSummaryOut.flush();

header = OccDetailCsvRecord.csvHeaderString();
log.debug( header );
occDetailOut.write( header );
occDetailOut.flush();

header = MrcSummaryCsvRecord.csvHeaderString();
log.debug( header );
mrcSummaryOut.write( header );
mrcSummaryOut.flush();

header = MrcDetailCsvRecord.csvHeaderString();
log.debug( header );
mrcDetailOut.write( header );
mrcDetailOut.flush();

openedFiles = true;
}

public void close() throws IOException
{
if( !openedFiles ) return; // make sure we've already opened the files before proceeding.

if( mrcSummaryOut != null ) {
mrcSummaryOut.flush();
mrcSummaryOut.close();
mrcSummaryOut = null;
}
if( occDetailOut != null ) {
occDetailOut.flush();
occDetailOut.close();
occDetailOut = null;
}
if( mrcSummaryOut != null ) {
mrcSummaryOut.flush();
mrcSummaryOut.close();
mrcSummaryOut = null;
}
if( occDetailOut != null ) {
occDetailOut.flush();
occDetailOut.close();
occDetailOut = null;
}
openedFiles = false;
}

}
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, John.
It would have been polite to give us a working sample of code, but with the addition of a main() method and commenting out the classes you didn't give us I was able to duplicate your problem. In your close() method you are flushing and closing the same two files twice. Flush and close all 4 files and you're in business.
 
John Kramer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,

My apologies for missing the finer points of posting the question. I do appreciate the extra trouble you went to to locate the problem.

Thanks for being an extra set of eyes!

John
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!