f



java.io.FileReader and java.io.FileWriter encoding

Hi,

I recently changed the hosting for my website (from Linux to Linux) and 
now I'm experiencing some trouble with java.io.FileReader and 
java.io.FileWriter: their getEncoding() method returns ASCII (returned 
ISO8859_1 on the previuos host). I've set -Dfile.encoding=ISO-8859-1 but 
nothing has changed, except that 
javax.mail.internet.MimeUtility.getDefaultJavaCharset() effectively 
returns ISO-8859-1.

Does anyone know how the default encoding is selected? I report some 
system properties:

file.encoding.pkg: sun.io
user.country: US
java.runtime.version: 1.4.2_04-b05
os.version: 2.4.27
user.timezone: America/New_York
file.encoding: ISO-8859-1
user.language: en
java.version: 1.4.2_04
java.vendor: Sun Microsystems Inc.

-----
Mario
0
9/17/2004 9:06:30 AM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

5 Replies
903 Views

Similar Articles

[PageSpeed] 46

Mario Maestro wrote:

> Hi,
> 
> I recently changed the hosting for my website (from Linux to Linux) and 
> now I'm experiencing some trouble with java.io.FileReader and 
> java.io.FileWriter: their getEncoding() method returns ASCII (returned 
> ISO8859_1 on the previuos host). I've set -Dfile.encoding=ISO-8859-1 but 
> nothing has changed, 

IMHO the pitfall is that you have to use "ISO8859-1"!

> except that 
> javax.mail.internet.MimeUtility.getDefaultJavaCharset() effectively 
> returns ISO-8859-1.

Don't mix up character encoding and file encoding - char encoding is a 
different story.

Matthias
0
9/17/2004 11:54:49 AM
Matthias Fraass wrote:
> IMHO the pitfall is that you have to use "ISO8859-1"!

The right name is ISO-8859-1 (see 
http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html).
I tried anyway (with ISO8859-1 and ISO8859_1) but it doesn't work.

I admit I'm a bit confused in general. By the way the file.encoding 
property shouldn't be a solution at all, since it wasn't set on my 
previous hosting (and everything was fine). On this one, nothing changes 
if I set it or not.

> Don't mix up character encoding and file encoding - char encoding is a 
> different story.

FileReader/Writer javadocs talk about "default character encoding". 
javax.mail.internet.MimeUtility.getDefaultJavaCharset() gets the 
system's default locale and returns the corresponding default Java charset.

When reading a file (written in bytes), Java needs to know which charset 
to use to convert 8-bit to 16-bit, and I need to know how the default 
one is picked up.
0
9/17/2004 12:31:05 PM
Mario Maestro wrote:
> When reading a file (written in bytes), Java needs to know which charset 
> to use to convert 8-bit to 16-bit, and I need to know how the default 
> one is picked up.

As an alternative, you can specify the encoding explicitly in InputStreamReader,
which is the preferred way.
0
brazil (1213)
9/17/2004 3:10:07 PM
Michael Borgwardt wrote:
> As an alternative, you can specify the encoding explicitly in 
> InputStreamReader,
> which is the preferred way.

I always do it, but the problem is still there with software written by 
others.

Mario
0
9/17/2004 3:31:39 PM
Mario Maestro wrote:
>> As an alternative, you can specify the encoding explicitly in 
>> InputStreamReader,
>> which is the preferred way.
> 
> 
> I always do it, but the problem is still there with software written by 
> others.

Then your only hope is to get -Dfile.encoding to work, because if the
conversion is done wrongly, it's not necessarily reversible.

0
brazil (1213)
9/17/2004 6:36:08 PM
Reply: