Question

I have one method that opens a file and passes off to another function to write some data to it. That second method wants to use PrintWriter to write its data. However, I didn't want to require that everybody use PrintWriter to write to that stream.

Currently it looks something like this ( sanitized example... don't bother criticizing my choice of method or variable names )

public void exportRawDataIntoStream(OutputStream os) {
    PrintWriter pr = new PrintWriter(os);
    printFirstData(pr);
    printSecondData(pr);
}

public void exportToFile(File file) {
    OutputStream os = null;
    try {
        os = new BufferedOutputStream(new FileOutputStream(file));
        exportRawDataIntoStream(os);
                doMoreWithTimeFile(os);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (Exception e ) {
                e.printStackTrace();
            }
        }
    }
}

This doesn't work, unless I put a 'pr.flush' at the end of exportRawDataIntoStream. I can't close the PrintWriter, because that closes the whole stream.

Is doing the flush legitimate and reliable? Is there some other approach I should use to mix Writers on the same stream, or should I just absolutely never do that?

Was it helpful?

Solution

Yes, flush should be reliable. It's basic role is to "push along" any excess data.

However, to be fair, that will only work with your basic Writers. Mind, I think that is most, if not all, of them.

But consider some contrived special kind of writer. Something that "does something else" on close. Like an encryption or compression stream.

Say, you had a "base64 encoder writer", it can't safely write the potentially trailing "==" until it is DONE, and that's a close, not a flush.

But with that caveat, for your case with the PrintWriter, it shouldn't be a problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top