473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 37348
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ann | last post by:
Hi, Is there any way to Change encoding of Java Vm to ISO-8859-1? i am using Java vm along with an application called opencms. I get the following error message.. Error: the encoding of your...
13
by: Nicholas Pappas | last post by:
Hello all. I am trying to write a Java3D loader for a geometry file from a game, which has Unicode characters (Korean) in it. I wrote the loader and it works in Windows, but I recently brushed...
30
by: aurora | last post by:
I have long find the Python default encoding of strict ASCII frustrating. For one thing I prefer to get garbage character than an exception. But the biggest issue is Unicode exception often pop up...
4
by: Kent Tong | last post by:
Hi, Why is it required to specify the encoding pseudo attribute in a DTD? For example, it is OK to have: <?xml version="1.0" encoding="ISO-8859-1"?> <!ELEMENT ...> But it causes Xerces to...
5
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... Have another thread going on in scripting.jscript trying to work around some deficiencies in the way IE and IIS interact. The nub of it is this: ASP.Net explicitly sets an output...
1
by: Bob Bryan | last post by:
Hi, I am writing a windows app in C# and referencing a lib built in J# and which I don't have the code for that requires a method to be overridden that contains a parameter defined as: ...
1
by: arunairs | last post by:
Hi, After writing the £ (pound sterling) symbol to a file using StreamWriter using the default encoding UTF8, when I retrieve it the file symbol £ gets changed. But when I use Encoding.Default in...
4
by: K Viltersten | last post by:
I noticed that when i read files from an FTP using FtpWebRequest, the file names using swedish characters seem not to be interpreted correctly. I get a square, instead. After having looked for...
3
by: K Viltersten | last post by:
I'll be retrieving file lists from different FTP's and i can't know for sure what encoding a certain server will use. How can i determine it so i can use it in my StreamReader object? --...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.