Character Encoding

Dear All,

I have a problem with reading and writing unicode files. here is the listing.

while((l=in.readLine())!=null){

out.println(l+"haha");

}

the file that i'm trying to read is encoded in 16 bit Unicode(not UTF-8) and the program also writes it to an output file with the same encoding. but all the added text parts("haha") are garbled. I tested the program with UTF-8 and ANSI files and it worked fine. But when I tried to use it with Unicode and Unicode big endian , the problem appeared. My conclusion is that it works fine for utf-8 and ANSI because they both use ASCII encoding for the English characters(which my text is composed of) and when it inserts them to Unicode files it doesn't translate it to Unicode. But, I've read it on Java tutorial that when we use FileReader and FileWriter stream classes, they translate the input characters to Unicode which is the encoding that Java uses and then translate it again to the other encoding when writing. But regarding my experiment I think Java doesn't translate characters to appropriate encoding(in my case 16 bit Unicode).

[1127 byte] By [amir_hossein_monshia] at [2007-11-27 10:15:08]
# 1

Unless you tell it otherwise Java assumes that text files are in the standard encoding set on your operating system. To set the encoding on input or output explicitly use InputStreamReader and OutputStreamWriter which take the encoding name as a second constructor parameter so:

Reader r = new InputStreamReader(new FileInputStream(inf), "UTF-16BE");

BufferedReader in = new BufferedReader(r);

malcolmmca at 2007-7-28 15:37:41 > top of Java-index,Java Essentials,Java Programming...
# 2

Thats it! Thank you very much. Cheers!

amir_hossein_monshia at 2007-7-28 15:37:41 > top of Java-index,Java Essentials,Java Programming...