By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

How to specify encoding in Java?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
> 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.