Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9190

Re: I/O Confusion

From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: I/O Confusion
Date 2011-10-25 18:47 -0700
Organization A noiseless patient Spider
Message-ID <j87orj$opq$1@dont-email.me> (permalink)
References <Xns9F89C3FFB48DDjpnasty@94.75.214.39>

Show all headers | View raw


On 10/25/2011 4:12 PM, Novice wrote:

> I also get very confused when I see Streams, Readers and Writers wrapped
> within one another. For example, this snippet confuses me:
>
> Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream
> ("outfilename"), "UTF8"));


This was confusing to me at first too, but it makes perfect sense if you 
think about it.  The problem tends to be that other languages shield you 
from some of this, while Java lets you assemble the low level bits how 
you want.

The thing to remember is everything on a disk is binary, until it gets 
interpreted as something else.  It the data could represent integer 
numbers, or an image, or characters.  So first at the lowest level you 
just want to write bytes.  That's what X_OutputStream and X_InputStream 
are for.

Next up the chain you might want to interpret the data as characters, 
not binary.  But there's a niggle, Readers and Writers only accept other 
Readers and Writers -- they expect the translation to be already done. 
So there's an intermediate class that does that translation. 
InputStreamReader/Write translate from and to Java character data to raw 
bits.

Why expose this detail to the user? I expect it was just to save the API 
writers from having to overload every single Reader and Write method 
with a pair of methods that manipulate raw streams, and also manipulate 
raw stream for a specified encoding.  It's just good software design, 
although it is rather verbose.

So in summary, Java always translates character data to and from raw 
streams.

   Characters <=============================> Raw Binary

Where the translator is call an InputStreamReader/OutputStreamWriter.

   Characters <=============================> Raw Binary
                  I/O Stream Reader/Writer

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

I/O Confusion Novice <novice@example..com> - 2011-10-25 23:12 +0000
  Re: I/O Confusion Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-25 21:20 -0400
  Re: I/O Confusion Silvio Bierman <silvio@moc.com> - 2011-10-26 03:21 +0200
  Re: I/O Confusion markspace <-@.> - 2011-10-25 18:47 -0700
  Re: I/O Confusion Roedy Green <see_website@mindprod.com.invalid> - 2011-10-26 08:55 -0700
    Re: I/O Confusion "Nasser M. Abbasi" <nma@12000.org> - 2011-10-26 12:03 -0500
      Re: I/O Confusion Novice <novice@example..com> - 2011-10-26 17:35 +0000
    Re: I/O Confusion Novice <novice@example..com> - 2011-10-26 20:57 +0000
      Re: I/O Confusion Roedy Green <see_website@mindprod.com.invalid> - 2011-10-26 16:48 -0700

csiph-web