Java string literal encoding issue (solved)

Answered

Hi,

I don't know how to solve this...

The string literals are displayed correctly in the editing window but are not correct when displayed in console or written in file or inserted in sql database.

If I use escape code everything is ok.

IDE file encoding is UTF8.

Code:

cnx.update( "update cim10 set cim_libelle_court=? where cim_code_pk=?", "Chol\u00e9ra", "A00" );
cnx.update( "update cim10 set cim_libelle_long=? where cim_code_pk=?", "Choléra", "A00" );
System.out.println( Charset.defaultCharset());
System.out.println( "Chol\u00e9ra ?= " + rs.getString( 3 ) ); // cim_libelle_court
System.out.println( "Choléra ?= " + rs.getString( 4 ) ); // cim_libelle_long

Console:

UTF-8
Choléra ?= Choléra
Choléra ?= Choléra

Log file:

APP .INFO |29/11/2016 15:50:55:790|00000001:app.Map.main(119) |Choléra ?= Choléra
APP .INFO |29/11/2016 15:50:55:790|00000001:app.MAp.main(120) |Choléra ?= Choléra

Environment infos:

IntelliJ IDEA 2016.3
Build #IC-163.7743.44, built on November 17, 2016
JRE: 1.8.0_112-release-408-b2 x86
JVM: OpenJDK Server VM by JetBrains s.r.o

Windows 7 - JDK 1.7 32 bits

Thanks for your help

 

0
5 comments
Avatar
Permanently deleted user

As often you find the answer on your own just after asking to everybody...

By checking the File encodings I clicked on the .idea directory and found the file encodings.xml.

I did not find any mention of this file before.

I don't know why but some of my project java files were registered in this encodings.xml file with "windows-1252" charset.

I have deleted all the lines and everything is working fine.

Anyway I don't understand in which circunstances this files were registered and why the IDE was displaying UTF-8 as file encoding.

0
Avatar
Permanently deleted user

May be I was too fast in my previous answer and it could be a bug somewhere in IJ.

In fact the class that I was testing WAS NOT in encodings.xml. This first observation explains why "UTF-8" was displayed in the IDE. And If I load one of the registered files I have "windows-1232" displayed in the IDE.

The "windows-1232" files are in the same package but not used at all by the Main class.

I reproduce the problem with two simple classes in a new project :

import java.nio.charset.Charset;

/**
* Created by jylaxx on 29/11/2016.
*/
public class Main
{
public static void main( String[] args ) throws Exception
{
System.out.println( Charset.defaultCharset() );
System.out.println( "chaîne accentuée" );
}
}
/**
* Created by jylaxx on 29/11/2016.
*/
public class Empty
{
}

The Empty class is encoded with windows-1252. and is regsitered in encodings.xml

The console is not OK :

UTF-8
chaîne accentuée

Quite strange...

0

Check individual file encodings, convert everything to UTF-8 and reset file specific encoding settings in IDE configuration.

0
Avatar
Permanently deleted user

May be I was not clear but the simple example is completely done in IJ. Both classes were created in IJ with default encoding (UTF-8) and then I changed the Empty class encoding to windows-1232.

I was just trying to find an explanation. Anyway my problem is solved.

0

Please sign in to leave a comment.