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


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

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

Date 2011-09-06 11:34 +0200
From Mayeul <mayeul.marguet@free.fr>
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 <4e65e822$0$14986$426a74cc@news.free.fr> (permalink)
Organization Guest of ProXad - France

Show all headers | View raw


On 26/08/2011 06:52, 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?

May I add to other answers, both of them create Writers which will use 
the environment's default charset.

If you would like to impose a charset to write with, let's say UTF-8, 
solution 1) provides a way for it:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new 
FileOutputStream("MyFile.txt"), "utf-8"));


While solution 2) does not. It probably should if you ask me, but it 
does not.

--
Mayeul

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-14 18:23 -0700
  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