472,096 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

How to specify encoding in Java?

I've been looking around and i've been given some hints but
i simply can't get to the bottom of it. How do i specify the
character encoding for reading text froma file?

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------


Jul 17 '05 #1
6 37213
Konrad Den Ende wrote:
I've been looking around and i've been given some hints but
i simply can't get to the bottom of it. How do i specify the
character encoding for reading text froma file?


Depends a bit on what you mean exactly by "read text from a file"; at some
point you have to convert bytes to chars (either by converting a byte[] to
a String or by wrapping an InputStream in a Reader), and you should find
that the method (or constructor) you used to do that is capable of taking a
parameter which specifies the encoding. If you don't give one then it will
use the default encoding, which IIRC is specified by the system property
"file.encoding"; which in turn probably defaults to "8859_1". If you want
to mix German and Chinese then you frobaly want to use "UTF-8".

Since this is 2004 (not to mention 4702) I am obliged to inform you that
you have much better control over character encoding if you use java.nio.
--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #2
Konrad Den Ende wrote:
I've been looking around and i've been given some hints but
i simply can't get to the bottom of it. How do i specify the
character encoding for reading text froma file?


Depends a bit on what you mean exactly by "read text from a file"; at some
point you have to convert bytes to chars (either by converting a byte[] to
a String or by wrapping an InputStream in a Reader), and you should find
that the method (or constructor) you used to do that is capable of taking a
parameter which specifies the encoding. If you don't give one then it will
use the default encoding, which IIRC is specified by the system property
"file.encoding"; which in turn probably defaults to "8859_1". If you want
to mix German and Chinese then you frobaly want to use "UTF-8".

Since this is 2004 (not to mention 4702) I am obliged to inform you that
you have much better control over character encoding if you use java.nio.
--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #3
Konrad Den Ende wrote:
I've been looking around and i've been given some hints but
i simply can't get to the bottom of it. How do i specify the
character encoding for reading text froma file?


Open the file as normal with an InputStream. Then use InputStreamReader
to wrap the stream, passing the desired encoding.

E.g., to open a file in UTF-16:

InputStream is = new BufferedInputStream(
new FileInputStream("unicode.txt"));
Reader reader = new InputStreamReader(is, "UTF-16");

See the javadoc for more information on which encodings are supported
and fancier ways of specifying them.

Ray
Jul 17 '05 #4
Konrad Den Ende wrote:
I've been looking around and i've been given some hints but
i simply can't get to the bottom of it. How do i specify the
character encoding for reading text froma file?


Open the file as normal with an InputStream. Then use InputStreamReader
to wrap the stream, passing the desired encoding.

E.g., to open a file in UTF-16:

InputStream is = new BufferedInputStream(
new FileInputStream("unicode.txt"));
Reader reader = new InputStreamReader(is, "UTF-16");

See the javadoc for more information on which encodings are supported
and fancier ways of specifying them.

Ray
Jul 17 '05 #5
> InputStream is = new BufferedInputStream(
new FileInputStream("unicode.txt"));
Reader reader = new InputStreamReader(is, "UTF-16");


Thanks. I'll try that right away.

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------


Jul 17 '05 #6
> InputStream is = new BufferedInputStream(
new FileInputStream("unicode.txt"));
Reader reader = new InputStreamReader(is, "UTF-16");


Thanks. I'll try that right away.

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------


Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ann | last post: by
13 posts views Thread by Nicholas Pappas | last post: by
30 posts views Thread by aurora | last post: by
5 posts views Thread by =?Utf-8?B?TWFyaw==?= | last post: by
1 post views Thread by arunairs | last post: by
3 posts views Thread by K Viltersten | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.