Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Print Unicode characters to the Terminal with Java' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Print Unicode characters to the Terminal with Java
Authored by: adrianm on Feb 11, '05 01:50:10PM
Alternatively, just run the original program like this:

java -Dfile.encoding=UTF8 Test
and it'll work fine.

Substitute UTF8 with whatever is appropriate for your current terminal/needs.

[ Reply to This | # ]

Print Unicode characters to the Terminal with Java
Authored by: gidds on Feb 11, '05 08:56:18PM

Yep, just what I was going to say.

Actually, there are several related issues here. (I've been through them coz I keep all my files in CP1252, aka Windows Latin-1.)

The main one is the encoding that the JVM uses for file I/O; as you say, you set this using the -D option to set the 'file.encoding' system property when running Java programs. This will affect all character-based -- FileReaders, FileWriters, &c -- unless they specify an alternate encoding (which isn't a good idea in general). Not FileInputStream &c, though, as they're byte-based.

Second is the encoding in your source code; you set this in the same way with the -D option when compiling. This will affect extended characters in string literals &c.

Third is the encoding used by whatever you edit files with. TextEdit can set this in its preferences, for example. Most command-line tools will use the environment variable LC_CTYPE, but there's a problem with 'vi' (at least in 10.2.8) which ignores it. (I've compiled my own version which sets it properly, by calling setlocale(LC_CTYPE, "") at the start.) If no locale is defined for the encoding you want, it's not hard to define your own using mklocale.

And fourth is the encoding used by the terminal itself. Terminal can set this in its Display preferences; iTerm in its Terminal Profiles. (I have a suspicion that extended characters used to cause occasional crashes in Terminal, but that may not apply to recent versions.)

If any of these differ, it'll cause problems at some point... But if they're all set up correctly, it'll handle extended characters just like 7-bit ones, and you'll never need to worry about the difference!

---
Andy/



[ Reply to This | # ]
Print Unicode characters to the Terminal with Java
Authored by: pjt33 on Feb 16, '05 03:24:20PM
Bad idea. Sun state that file.encoding is supposed to be read-only, so that gives non-portable code. In particular, it doesn't work with Sun's Linux VM.

[ Reply to This | # ]
Print Unicode characters to the Terminal with Java
Authored by: dc_rees on Aug 25, '05 05:22:30AM

Well, Sun is just plain wrong on this one.

Apple's java is not quite right either, perhaps. Java should get it's environment from the terminal when invoked from the terminal.

But wrapping your readers and writers to force them to read and write shift-JIS is putting that sort of functionality in the wrong place. When the terminal is not quite correctly interfaced with Java is exactly the time you don't want to hard code the work-around.



---
Say yes to CPU multiculture



[ Reply to This | # ]