DataOutputStream # writeBytes (String) vs BufferedWriter # write (String)

I would like to create an HTML file for my report. Report content can be created either using BufferedWriter#write(String)

 File f = new File("source.htm"); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); bw.write("Content"); 

or using DataOutputStream#writeBytes(String)

 File f = new File("source.htm"); DataOutputStream dosReport = new DataOutputStream(new FileOutputStream(f)); dosReport.wrtiteBytes("Content"); 

Is one of them better than the other? Why is this so?

+6
source share
3 answers

If you are writing text, you should use Writer , which handles the conversion from Unicode characters (the internal representation of Java strings) to the appropriate character encoding, such as UTF-8. DataOutputStream.writeBytes simply prints the lower eight bits of each char to a string and completely ignores eight high order bits - this is equivalent to UTF-8 for ASCII characters with codes below 128 (U + 007F and below) but almost certainly wrong for anything other than ASCII .

Instead of FileWriter, you should use OutputStreamWriter so that you can select a specific encoding (FileWriter always uses the standard encoding of the platform, which varies from platform to platform):

 File f = new File("source.htm"); BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(f), "UTF-8")); bw.write("Content"); 
+14
source

First, the DataOutputStream in your second example does not serve any useful purpose 1 . Indeed, if your lines contain characters that do not fit into 8 bits, the writeBytes(String) method will distort the text. Get rid of this. Data streams are designed to read and write fine-grained binary data. For simple bytes, use a regular (or buffered) input or output stream.

Secondly, in this particular use case, when you write all the output, this is one write operation, BufferedWriter also does not add value.

So in this case. you should compare:

  File f = new File("source.htm"); Writer w = new FileWriter(f); w.write("Content"); 

vs

  File f = new File("source.htm"); OutputStream os = new FileOutputStream(f); os.write("Content".getBytes()); 

In my opinion, the first version looks simpler and cleaner. And it's better to use Reader and Writer stacks for text input / output ... because they are intended for them. (They take care of encoding and decoding problems, clean and transparent.)

You can compare them if you really need to know which is faster (on your system!), But I suspect that there is not much difference ... and that the first version is faster.

1 - I think DataOutputStream has buffering under covers, but for this use case, buffering does not help performance.


In cases where you are executing multiple (small) records, rather than large ones, there is a significant performance advantage when using BufferedWriter (or BufferedOutputStream ) instead of an unbuffered writer or stream.


Another point is that both versions of your code use the default character encoding for the platform to encode the output file. It may be more appropriate to use a specific encoding regardless of the default value or to make it a configuration parameter or command line.

+1
source

OutputStream:

This abstract class is the superclass of all classes representing the output stream of bytes. The output stream takes output bytes and sends them to some receiver.

Applications that need to subclass OutputStream should always provide at least a method that writes one byte of output.

For instance:

 OutputStream os = new FileOutputStream("test.txt"); 

Bufferedwriter

Writes text to a character output stream, character buffering to ensure efficient writing of single characters, arrays and strings. A buffer size can be specified or a default size can be accepted. The default value is large enough for most purposes.

A newLine() provides a method that uses its own concept of a line separator platform, as defined by the line.separator system property. Not all platforms use a newline character ('\ n') to end lines. Therefore, calling this method to complete each output line is preferable to write a newline character directly.

In general, Writer immediately sends its output to a base character or stream of bytes. If an invitation is not required, we recommend wrapping the BufferedWriter around any Writer whose write() operations can be expensive, such as FileWriters and OutputStreamWriters .

For instance:

  PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out"))); 
+1
source

Source: https://habr.com/ru/post/959242/


All Articles