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

M

Mario Maestro

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.
 
M

Matthias Fraass

Mario said:
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
 
M

Mario Maestro

Matthias said:
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.
 
M

Michael Borgwardt

Mario said:
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.
 
M

Mario Maestro

Michael said:
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
 
M

Michael Borgwardt

Mario said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top