Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Reading files encoded in ANSI and UTF-8(Inside Eclipse and outside Eclipse)
Reading files encoded in ANSI and UTF-8 [message #677093] Tue, 07 June 2011 14:52 Go to next message
freetochoose is currently offline freetochooseFriend
Messages: 19
Registered: May 2011
Junior Member
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 #677114 is a reply to message #677093] Tue, 07 June 2011 15:36 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Rich Kulp

Hi,

What API are you using to read the file? Are you using just
new FileReader(file)?

You can tell the reader to use UTF-8 specifically all of the time. Then
it can read just ANSI or UTF-8 since ANSI is a subset of UTF-8.

If using FileReader, instead use:

new BufferedReader(new InputStreamReader(file, CharSet.forName("UTF-8")));


--
Thanks,
Rich Kulp
Re: Reading files encoded in ANSI and UTF-8 [message #677279 is a reply to message #677114] Wed, 08 June 2011 02:29 Go to previous messageGo to next message
freetochoose is currently offline freetochooseFriend
Messages: 19
Registered: May 2011
Junior Member
Thanks a lot. I am using BufferedReader.
Can I do that too?

ftc
Re: Reading files encoded in ANSI and UTF-8 [message #677284 is a reply to message #677279] Wed, 08 June 2011 02:54 Go to previous message
freetochoose is currently offline freetochooseFriend
Messages: 19
Registered: May 2011
Junior Member
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: Wed, 08 June 2011 02:55]

Report message to a moderator

Previous Topic:Exporting java runnable jar file problem
Next Topic:Import a war file through code
Goto Forum:
  


Current Time: Tue Mar 19 06:00:17 GMT 2024

Powered by FUDForum. Page generated in 0.02852 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top