aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Load Unicode Filecontent to JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Load Unicode Filecontent to JTextArea" Watch "Load Unicode Filecontent to JTextArea" New topic
Author

Load Unicode Filecontent to JTextArea

Daniel Palombo
Greenhorn

Joined: Feb 28, 2006
Posts: 10
Hello Ranchers,
I have a big problem with a little JTextArea.

When setting a text like this in a JTextArea, all looks fine:



But when I try to load this text from a file I get nonsense (square characters).

You may ask: How do you get this text to a file? I copy & paste it from the JTextArea. Then (in Windows XP) I generate a new textdocument with the default notepad. The unicode characters show here fine as well. At the moment I want to save it, I get the choise to save it in ANSI, Unicode, Unicode Big Endian or UTF-8. I choose Unicode.

To load the text to my JTextArea I do this:


Hmm, that's too simple I guess.
So, what might the magical lines be to get this rigth???

Thank you for your help!
Daniel Palombo
Randall Kippen
Greenhorn

Joined: Oct 18, 2005
Posts: 15
You should have something like:

InputStreamReader isr = new InputStreamReader(new FileInputStream(file), charset);

where charset is the same as what you used in notepad to save.
If you don't specify a charset, the default is something like ISO-8859-1.
Hence, the encoding/decoding doesn't match and it turns into gibberish.
Daniel Palombo
Greenhorn

Joined: Feb 28, 2006
Posts: 10
Yeepyyy!
It works!

I begin to feel comfortable here

There is one thing that disturbs me a little bit: It's me (or the hard coded program) that has to know, in which charset the filecontent is. It wood be nicer if I could 'automagically' figure out the charset of the file and provide this to the InputStremReader ... (because I don' really know if it's in uinicode, Big Endian, Little Endian etc.)

Thank you very much for your help! Really!!

Randall Kippen
Greenhorn

Joined: Oct 18, 2005
Posts: 15

There is one thing that disturbs me a little bit: It's me (or the hard coded program) that has to know, in which charset the filecontent is. It wood be nicer if I could 'automagically' figure out the charset of the file and provide this to the InputStremReader ... (because I don' really know if it's in uinicode, Big Endian, Little Endian etc.)

Thank you very much for your help! Really!!


If you are using fairly standard UTF encodings, then I think the following code should work. However, there is nothing that I know of that will detect every enoding. Also, when detecting the encoding, you will probably want to use a PushbackInputStream.

Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Daniel Palombo:
You may ask: How do you get this text to a file? I copy & paste it from the JTextArea. Then (in Windows XP) I generate a new textdocument with the default notepad. The unicode characters show here fine as well. At the moment I want to save it, I get the choise to save it in ANSI, Unicode, Unicode Big Endian or UTF-8. I choose Unicode.


You may have wanted to just choose UTF-8 here. Everything
else may have just worked, depending on the details.

see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8


bitguru blog
Daniel Palombo
Greenhorn

Joined: Feb 28, 2006
Posts: 10
@Randall Kippen
Your code example is just wat brings me nearer to paradise !!!

@Brian Cole
I think I have to deepen my knowledge about this topic ... Thank you for the good link

Thanx again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Load Unicode Filecontent to JTextArea