Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From a Java program, portable way to write strings with accents

Hullo,

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

My Java source files are in Unicode — which is good. In them, I have lines like this :

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

I use Eclipse to package the program as a JAR archive.

Then, the program is run by a command like this :

java -jar MyProgram.jar parameters

In the Windows XP command line, this gives :

ParamÞtre manquant. … 

Is there a portable way to write strings with accents in the Java program, so that they appear correctly in the Windows command line ? Or do we just have to live with Windows stupidly replacing accented E with icelandic thorn ?

I use Java 6.

like image 295
Nicolas Barbulesco Avatar asked Dec 26 '22 05:12

Nicolas Barbulesco


2 Answers

In Java 1.6 you can use System.console() instead of System.out.println() to display accentuated characters to 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 output is

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

See also Output accentuated characters to the console

like image 104
RealHowTo Avatar answered Dec 28 '22 18:12

RealHowTo


Use unicode escaped sequence: \u00E8

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

Here's an useful Unicode character table.

like image 37
Alepac Avatar answered Dec 28 '22 20:12

Alepac