0

Hello everyone I'm in my second semester of CS and we are on the subject of file IO using InputStreams and OutputStreams, everything was relatively simple until this subject for me. I am a little confused with the BufferedOutputStream class. I understand that it stores data in a buffer of the specified size, then writes it all at once to be more efficient than a byte by byte stream. What I do not understand is that, unlike DataInputStream, which has methods to write specific primitives, I can only write byte arrays. How would I store primitives like int, long, double, etc.. using the BufferedOutputStream. Thank you guys in advance!

Philip Verso
  • 23
  • 1
  • 5
  • The answer to the dupe gives you a pretty good idea of how `DataOutputStream` and `BufferedOutputStream` should be used together. – kdgregory Apr 22 '18 at 14:11

2 Answers2

0

You could use guava library for converting primitves to byteArray. It has pretty sweet syntax:

byte[] bytearray = Ints.toByteArray(201);

where 201 is the primitive integer you wish to use. Since now you have byteArray you can easily use it in BufferedOutputStream.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
0

Wrap a DataOutputStream around it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @Set Actually it does provide an answer to the question, as anyone who knows anything about the classes mentioned would know: and there is nothing about this answer that suggests for a moment that further clarification is required or requested. Your comment is beyond ridiculous. – user207421 Apr 21 '18 at 10:25
  • I didn't downvote, but although this answer is correct it's not very good. IMO a good answer would explain that the Java input/output streams are used as decorators, and would give an example of how the `DataOutputStream` would wrap a `BufferedOutputStream`, which would wrap some underlying stream. A better answer would go into detail on how `close()` is propagated, and also the use of a `finally` block to ensure that the stream is flushed or closed, as appropriate (and when one or the other is appropriate). – kdgregory Apr 21 '18 at 17:11
  • Better still, however, would be to fine a relevant duplicate and mark the question as such. – kdgregory Apr 21 '18 at 17:12
  • @kdgregory If he is already using `BufferedOutputStream` he must already know about the decorator pattern issue. – user207421 Apr 21 '18 at 23:09
  • @EJP, I believe the conciseness of your answer may have sparked the negative reaction, even though this answer is eminently correct. Maybe quoting and linking to the corresponding Javadoc comment would help? – Lolo Apr 22 '18 at 06:14
  • 1
    @Lolo That's just the usual SO nonsense. The Javadoc is there to be looked up whether I link to it or quote it or not. Links can break, and quotations can be accurate or inaccurate, or obsolete. The fact is that five words is all it takes to answer this question. Brevity is the soul of wit. A delete vote for a correct answer is evidence of a problem, but not in the answer. – user207421 Apr 22 '18 at 10:10
  • Re "he must already know about the decorator pattern": I've reread the question several times, and it seems obvious to me that that's exactly the OP's problem. – kdgregory Apr 22 '18 at 14:08
  • @kdgregory It is obvious to everybody else that what he actually says is that he can only write byte arrays with it. He doesn't express or imply any difficulty with the decoration aspect. The answers in the duplicate you linked to say exactly the same thing as this answer. You can't have it both ways, – user207421 Aug 09 '19 at 09:52