Reading files encoded in ANSI and UTF-8 [message #677093] |
Tue, 07 June 2011 10:52  |
Eclipse User |
|
|
|
Hi:
I posed a question regarding running the same program inside Eclipse and outside
Eclipse with different behavior. I have a program that reads a text file with
Chinese characters.
When I run the program inside Eclipse, if the file is UTF-8 encoding, it is read
correctly and if the file is ANSI encoded, it does not read it correctly.
But now if I export the same program to a runnable jar file and run it outside
of Eclipse. This time, if the file is ANSI encoded, it is read correctly and
if it is UTF-8 encoded, it is not.
Of course, it the file just contains normal alphabets and numbers, the program
will read it correctly inside or outside Eclipse without regard to its encoding.
Which means my program reads ANSI files and UTF-8 files inside Eclipse correctly
if the ANSI file does not contain Chinese characters.
Now I need to encode my file with UTF-8 so my program will read it correctly inside
Eclipse. Once I decided that my program is OK and export to an external jar file,
I need to use something like Notepad++ to encode my file to ANSI so my program
can read it correctly outside Eclipse. That seems to be a lot of hassle.
Does anyone know a better way?
Thanks a lot in advance.
ftc
|
|
|
|
|
Re: Reading files encoded in ANSI and UTF-8 [message #677284 is a reply to message #677279] |
Tue, 07 June 2011 22:54  |
Eclipse User |
|
|
|
If I use BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName"UTF-8")));
It works well for utf-8 files both inside Eclipse and outside Eclipse.
thanks a lot.
[Updated on: Tue, 07 June 2011 22:55] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.08413 seconds