FileWriter problem?

Hi,

I tried to write characters in a file using FileWriter whose int value is above 255 but always I get? written in the file. Why is it so?

Code below:

publicclass FileTest{

publicstaticvoid main(String args[])throws Exception{

FileWriter fw =new FileWriter("write1.txt");

int i = Integer.parseInt(args[0]);

fw.write(i);

fw.close();

}

}

When I run above class by passing command line argument upto 255, it writes the ISO Latin-1 characters in the file. But when I provide any value after 255, it always writes?

I also compiled above program byjavac -encoding UTF-8 FileTest.java , even in that case it writes the same ? in the file. With-encoding UTF-16 , it gives me compilation error around 100 errors or so?

Can you please tell me what is going wrong? How can I print all the unicode characters?

I am using java 1.5 on Windows 2000 professional OS. I also found the default character set on my machine, it gives me Windows-1252

Any pointer would be highly appreciated.

Thanks in advance

Naseem

[1538 byte] By [nakhan81a] at [2007-10-3 4:42:55]
# 1
It doesn't necessarily write ?. It might be that the tool you're using to read the file doesn't process the unicode characters properly, or the font that it's using to display them doesn't have entries for those chars.
jverda at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 2

> It doesn't necessarily write ?. It might be

> that the tool you're using to read the file doesn't

> process the unicode characters properly, or the font

> that it's using to display them doesn't have entries

> for those chars.

Oh is it so? Thanks for the prompt reply. Well I opened the file in notepad and gives the ?. I opened it on microsoft word as well and it shows the same character.

Does it mean that the required font is not installed on my machine?

If font is causing all problem, so can I get all unicode fonts on my machine.

Thanks once again

Naseem

nakhan81a at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 3
You are writing binary to the file, an integer. You shouldn't be expecting to see anything sensible via Notepad or any other text editor. It's not text.Do you need to write the integer in binary or as a string?
ejpa at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 4

> You are writing binary to the file, an integer. You

> shouldn't be expecting to see anything sensible via

> Notepad or any other text editor. It's not text.

>

> Do you need to write the integer in binary or as a

> string?

There must be some character which corresponds to 256 just like we have 65 for A, 223 for beta. I just want to write that character which corresponds to 256. Anything above 255.

Naseem

nakhan81a at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 5

> You are writing binary to the file, an integer. You

> shouldn't be expecting to see anything sensible via

> Notepad or any other text editor. It's not text.

>

> Do you need to write the integer in binary or as a

> string?

If that int happens to be the codepoint (Is that the right term? I always get the Unicode terminology messed up.) for a UTF-8 character, then it will be, won't it?

Writer's javadoc says:

public void write(int c)

throws IOException

Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

jverda at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 6

> > You are writing binary to the file, an integer.

> You

> > shouldn't be expecting to see anything sensible

> via

> > Notepad or any other text editor. It's not text.

> >

> > Do you need to write the integer in binary or as a

> > string?

>

> There must be some character which corresponds to 256

Not necessarily 256, but some of the larger numbers, yes. (Not every value is a valid character.)

jverda at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 7

So what is the solution of it? Suppose if I want to read or write say russian characters or chinese, how should I write those characters?

I just downloaded Devanagari font on my machine. It has a character spelled as ka and its code point is U+0915. Since 0915 is in hexadecimal representation, i just converted it in decimal representation which is 2325. Now if I write this number in the file, I should get my original character ka. When I opened the file in the text editor which supports that font, its not showing me the same character rather it always shows a fixed character.

Comments please?

Thanks

Naseem

nakhan81a at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 8

I've never written character by character with the write(int) method you're using. It looks like it should work, but I'm not completely sure.

I'd try writing out a String, rather than an int.

Also, just having a font that supports the character may not be enough. Can you open other files written in that script using that text editor?

jverda at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 9

> I've never written character by character with the

> write(int) method you're using. It looks like it

> should work, but I'm not completely sure.

>

> I'd try writing out a String, rather than an int.

Obivously its not the right approach. I just want to see the output. That it. Going for BufferedWriter is better than writing single character.

> Also, just having a font that supports the character

> may not be enough. Can you open other files written

> in that script using that text editor?

No. I just checked one website that is in the same font. I copied the content in the file editor but it shows some junk characters even after the selection of that font. :(

Naseem

null

nakhan81a at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 10

> > Also, just having a font that supports the

> character

> > may not be enough. Can you open other files

> written

> > in that script using that text editor?

>

> No. I just checked one website that is in the same

> font. I copied the content in the file editor but it

> shows some junk characters. :(

That's actually a good sign as far as your Java code goes though. It means the code might be fine, and the problem might be entirely with something else.

If you're on XP, try this: Control Panel --> Regional and Language Options --> Languages tab --> Details.

If the language you want is not listed in the panel under Installed Services, click Add, and try to add it.

Also, not every editor will support unicode, even if you're using a font that has the right characters, so you might have to do some research there.

jverda at 2007-7-14 22:46:56 > top of Java-index,Core,Core APIs...
# 11
Thanks for your suggestions.I will try it as you told and as far as encoding is concerned, I just downloaded one text editor which in fact supports almost all encoding.Here is the url: http://www.wolosoft.com/en/superedi/RegardsNaseem
nakhan81a at 2007-7-14 22:46:57 > top of Java-index,Core,Core APIs...
# 12
You're welcome, and good luck.I don't know all that much about the details of Unicode. I've read throught the basics, and mucked with it a bit, but it's never been enough of an issue for me to learn it thoroughly. I suspect that will change before too long though. :-)
jverda at 2007-7-14 22:46:57 > top of Java-index,Core,Core APIs...
# 13

Hi friends,

Luckily I am able to write some Arabic characters in the file. I encoded Unicode string as a UTF8 byte array and wrote the resulting byte array in the file.

Here is the code:

String str= new String("\u06F3");

FileOutputStream fos = new FileOutputStream("test.txt");

Writer out = new OutputStreamWriter(fos, "UTF8");

out.write(str);

out.close();

Output: Arabic digit 3 written in file

Thanks to all for your valuable inputs

Naseem

nakhan81a at 2007-7-14 22:46:57 > top of Java-index,Core,Core APIs...