read unicode from file

G

gert

I want to read a unicode String from a file:
FileInputStream stream = new FileInputStream(file);

DataInputStream din = new DataInputStream(stream);
byte[] y = new byte[6];
din.read(y);
String unit = new String(y, "UTF-8");
din.close();

If the file contains the entry \u00B0 I would expect if displaying unit
on a JLabel see the 'degree' sign (°).
But I see this: \u00B0
If I do the following: String unit = "\u00B0" an display this unit on a
JLabel it displays the '°' correctly.
What do I do wrong??
Many thanks for any help
 
T

Thomas Kellerer

I want to read a unicode String from a file:
FileInputStream stream = new FileInputStream(file);

DataInputStream din = new DataInputStream(stream);
byte[] y = new byte[6];
din.read(y);
String unit = new String(y, "UTF-8");
din.close();

If the file contains the entry \u00B0 I would expect if displaying unit
on a JLabel see the 'degree' sign (°).
But I see this: \u00B0
If I do the following: String unit = "\u00B0" an display this unit on a
JLabel it displays the '°' correctly.
What do I do wrong??

Read the stuff using a Reader do not use a plain InputStream

Reader r = new InputStreamReader(new FileInputStream(file), "UTF-8"));

Thomas
 
S

Simon

Thomas said:
I want to read a unicode String from a file:
FileInputStream stream = new FileInputStream(file);

DataInputStream din = new DataInputStream(stream);
byte[] y = new byte[6];
din.read(y);
String unit = new String(y, "UTF-8");
din.close();

If the file contains the entry \u00B0 I would expect if displaying unit
on a JLabel see the 'degree' sign (°).
But I see this: \u00B0
If I do the following: String unit = "\u00B0" an display this unit on a
JLabel it displays the '°' correctly.
What do I do wrong??

Read the stuff using a Reader do not use a plain InputStream

Reader r = new InputStreamReader(new FileInputStream(file), "UTF-8"));

Apart from that I understand that the file contains a literal backslash, a "u"
and four digits (as opposed to the file containing the byte sequence that is
represented by "00B0" in hex). Otherwise an InputStream would still not produce
the described output. If this is true, using a Reader will not help.

In that case you can refer to the following article, where it was discussed how
you can parse strings of your type:

news://News.CIS.DFN.de:119/[email protected]

Cheers,
Simon
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top