Can I set the default encoding for filewriter?
Hi all,
I have used the following line to write into a file.
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File(filename.txt)));
Now, if I write Russian text into the file, It gives me only question marks.
I know the problem is because of the Filewriter, which uses the default encoding of the system, which would not support Russian and I could use FileOutputStream/OutputStreamWriter and specify the encoding.
But I was wondering if I could change the jvm settings, to say UTF -8, so that Filewriter would assume the same? Is this okay or will I land up messing something else?
Please help.
[665 byte] By [
sami@dakua] at [2007-11-26 13:16:31]

# 2
Or if you just want this particular Writer to use the correct encoding, you could do this:BufferedWriter bufferedWriter =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename.txt), encoding);
# 4
> Try using this:
> [code]System.setProperty("file.enconding","UTF-8");[/c
> ode] before creating the bufferedWriter.
Although this might work for you, I personally would counsel you against setting this property. The property is (roughly) supposed to reflect the default system encoding for your host system. Unfortunately, there are a few different encodings available at any time, and this property's purpose is maybe not as clear as it should be. Nevertheless, I suggest you use explicit encodings everywhere. Go ahead and set your own global application property somehow, and use that when creating writers, readers, etc...but I'd avoid changing system property values.