Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: I/O Confusion Date: Tue, 25 Oct 2011 18:47:27 -0700 Organization: A noiseless patient Spider Lines: 42 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 26 Oct 2011 01:47:31 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="25402"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184zG2587OZcDQ6ycl/dTZP9uN/8PVYnIY=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 In-Reply-To: Cancel-Lock: sha1:znAWVVUXlsGV2wKGR5UPEBbqyvM= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9190 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