Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

98 views
Skip to first unread message

Mario Maestro

unread,
Sep 17, 2004, 5:06:30 AM9/17/04
to
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

Matthias Fraass

unread,
Sep 17, 2004, 7:54:49 AM9/17/04
to
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

Mario Maestro

unread,
Sep 17, 2004, 8:31:05 AM9/17/04
to
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.

Michael Borgwardt

unread,
Sep 17, 2004, 11:10:07 AM9/17/04
to
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.

Mario Maestro

unread,
Sep 17, 2004, 11:31:39 AM9/17/04
to
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

Michael Borgwardt

unread,
Sep 17, 2004, 2:36:08 PM9/17/04
to

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

0 new messages