From a Java program, a portable way to write accent strings

Hello,

I have a java program with a command line interface. It is used on Linux and Windows. Java code is portable, and I want it to stay portable.

My Java source files are in Unicode - that's good. In them, I have lines like this:

System.err.println("Paramètre manquant. … "); 

I am using Eclipse for the package as a JAR archive.

The program then starts with a command like this:

java -jar MyProgram.jar parameters

At the Windows XP command prompt, this gives:

ParamÞtre manquant. …

Is there a portable way to write accent lines in a Java program so that they display correctly on the Windows command line? Or do we just need to live with Windows, stupidly replacing the accented E with an Icelandic spike?

I am using Java 6.

+4
source share
3 answers

In Java 1.6, you can use System.console () instead of System.out.println () to display the selected characters in the console.

 public class Test2 { public static void main(String args[]){ String s = "caractères français : à é \u00e9"; // Unicode for "é" System.out.println(s); System.console().writer().println(s); } } 

and the way out is

 C:\temp>java Test caractþres franþais : Ó Ú Ú caractères français : à é é 

See also Output selected characters to the console.

+5
source

Use unicode escape sequence: \u00E8

 System.err.println("Param\u00E8tre manquant. … "); 

Here is a useful Unicode character table.

+3
source

Try using \u<XXXX> when encoding Unicode characters. It will not look beautiful in code, but it will work and be portable.

For instance:

 String specialCharacters= "\u00E1 \u00E9 \u00ED \u00F3 \u00FA"; System.out.println(specialCharacters); // This will print á é í ó ú 

Check Alepac's answer to the unicode character table.

+1
source

All Articles