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


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

Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ?

Date 2011-08-26 09:22 +0200
From Silvio <silvio@moc.com>
Newsgroups comp.lang.java.programmer
Subject Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ?
References <4e57267d$0$6552$9b4e6d93@newsspool4.arcor-online.net>
Message-ID <4e57499b$0$2549$e4fe514c@news2.news.xs4all.nl> (permalink)

Show all headers | View raw


On 08/26/2011 06:52 AM, Jochen Brenzlinger wrote:
> As you know there are two ways of writing text into a text file:
>
> 1.) BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("MyFile.txt")));
>      out.write(s);
>
> and
>
> 2.) BufferedWriter out = new BufferedWriter(new FileWriter("MyFile.txt"));
>      out.write(s);
>
> Whats the difference?
>
> Jochen
>

In java.io you either do byte IO or character IO. Byte IO can be used 
for any form of data without any interpretation of what that data 
represents. Character IO is meant for, well, information that is 
represented as sequences of characters.

For byte IO we have various types of InputStream/OutputStream classes 
like the FileOutputStream from your example 1). For character data whe 
have various forms of Reader/Writer classes like the FileWriter from 
your example. Buffering can be done in both cases, hence the 
BufferedWriter and BufferedOutputStream classes.

To do character IO characters will at some point have to be converted to 
byte data using some form of encoding. FileWriter does that for you, 
which is why it has a constructor that takes the encoding along with the 
file name. Common encodings include UTF-8, UTF-16 and US-ASCII.

OutputStreamWriter is a class that only does the encoding of characters. 
That is why it is a Writer and its constructor takes an OutputStream as 
parameter with an optional encoding parameter.

If you omit the explicit encoding specifications both FileWriter and 
ByteArrayOutputStream will use the default encoding for your platform. 
This is something to be very careful with since it will vary among 
different JVMs.

A FileWriter could be seen as an OutputStreamWriter wrapped around a 
FileOutputStream. That explains why your examples are quite similar.

Gr. Silvio

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


Thread

Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? jochen2@brenz.com (Jochen Brenzlinger) - 2011-08-26 04:52 +0000
  Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Silvio <silvio@moc.com> - 2011-08-26 09:22 +0200
  Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Lew <lewbloch@gmail.com> - 2011-08-26 00:54 -0700
  Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Roedy Green <see_website@mindprod.com.invalid> - 2011-08-31 14:13 -0700
  Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Arne Vajhøj <arne@vajhoej.dk> - 2011-09-05 21:17 -0400
  Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Mayeul <mayeul.marguet@free.fr> - 2011-09-06 11:34 +0200
  Re: Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Roedy Green <see_website@mindprod.com.invalid> - 2011-09-06 06:53 -0700

csiph-web