file io - Switching Writers on an OutputStream in Java -


I have a method that opens the file and closes it to write some data in some data This second The way the printer wishes to use to write data However, I did not want everyone to use the print-wighter to write that stream.

Currently it looks like something (clean example ... do not criticize my choice of method or variable names)

  Public Zero Export RawDataInStream (OutputStream OS) {PrintWriter public relations = new PrintWriter (OS); PrintFirstData (public relations); PrintSecondData (public relations); } Public Zero Export tofile (file file) {outputstream OS = null; Try {Os = new BufferedOutputStream (New FileOutputStream (file)); ExportRawDataIntoStream (OS); DoMoreWithTimeFile (OS); } Finally {if (os! = Null) {try {os.close (); } Hold (exception e) {e.printStackTrace (); }}}}  

This does not work, unless I put 'pr.flush' at the end of exportRawDataInStream. I can not close PrintWriter because it closes the whole stream

Is the flush valid and reliable? Is there any other approach that I should use to mix the authors on the same stream, or should not I do this at all?

Yes, the flush should be trusted. It's the basic role that "push with any additional data".

However, to be fair, it will only work with your original authors. Mind, I think most, if not, all of them.

But consider some specific types of authors. Something "something else" on something like encryption or compression stream. Say, you had "Base 64 Encoder Author", it probably can not write securely until it is run until "==", and it's a close, not flush.

But with that warning, for your case with PrintWriter, it should not be a problem.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -