0

Here is the code snippet:

StreamConnection st_connect=(StreamConnection)Connector.open(connectionURL);
OutputStream outStream=st_connect.openOutputStream();
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
pWriter.write("Test\r\n");

From what I know OutputStream is used with PrintStream. If that is true, then why is a PrintWriter reference used in this case? For your reference, this is a part of a Bluetooth Client Application based on SPP .

Flame of udun
  • 2,136
  • 7
  • 35
  • 79
  • What do you mean by "OutputStream is used with PrintStream"? This is constructing an `OutputStreamWriter` around the `OutputStream`, and then wrapping that in a `PrintWriter`. It's not really clear what you're asking. – Jon Skeet Aug 24 '13 at 17:27
  • @JonSkeet The top rated answer in this question [link](http://stackoverflow.com/questions/2822005/java-difference-between-printstream-and-printwriter) says that "`PrintStream` prints to an `OutputStream`"...this is what I was referring to – Flame of udun Aug 24 '13 at 17:30
  • But why is that relevant? There *isn't* a `PrintStream` here. There could have been, but maybe the author preferred the interface exposed by `PrintWriter`. Personally I don't like either of them (they swallow exceptions) and I don't like using an `OutputStreamWriter` without specifying an encoding, but at least `PrintWriter` adheres to the idea that `Writer` subclasses deal with text whereas `OutputStream` subclasses deal with binary data. `PrintStream` is a design smell, IMO. It's not clear why you think a `PrintWriter` *shouldn't* be used here. – Jon Skeet Aug 24 '13 at 17:32
  • @JonSkeet I hadn't seen `PrintWriter` in use before.So I thought it served some specific with respect to bluetooth spp communication and wanted to know it if there was one. – Flame of udun Aug 24 '13 at 17:37
  • No, it's absolutely not Bluetooth-specific. It's just a `Writer`-based equivalent of `PrintStream`. – Jon Skeet Aug 24 '13 at 17:38

0 Answers0