2

I am trying to pass Arabic String into Function that store it into a database but the String's Chars is converted into '?'

as example

String str = new String();
str = "عشب";
System.out.print(str);

the output will be :

"???"

and it is stored like this in the database.

and if i insert into database directly it works well.

ScreenShot of the code and result

4 Answers4

2

Make sure your character encoding is utf-8.

The snippet you showed works perfectly as expected.

For example if you are encoding your source files using windows-1252 it won't work.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
1

The problem is that System.out.println is PrintWriter which converts the Arabic string into bytes using the default encoding; which presumably cannot handle the arabic characters. Try

System.out.write(str.getBytes("UTF-8"));
System.out.println();
Kathy Van Stone
  • 25,531
  • 3
  • 32
  • 40
1

Many modern operating systems use UTF-8 as default encoding which will support non-latin characters correctly. Windows is not one of those, with ANSI being the default in Western installations (I have not used Windows recently, so that may have changed). Either way, you should probably force the default character encoding for the Java process, irrespective of the platform.

As described in another Stackoverflow question (see Setting the default Java character encoding?), you'll need to changed the default as follows, for the Java process:

java -Dfile.encoding=UTF-8

Additionally, since you are running in IDE you may need to tell it to display the output in the indicated charset or risk corruption, though that is IDE specific and the exact instructions will depend on your IDE.

One other thing, is if you are reading or writing text files then you should always specify the expected character encoding, otherwise you will risk falling back to the platform default.

Andre M
  • 6,649
  • 7
  • 52
  • 93
1

You need to set character set utf-8 for this.

at java level you can do:

Charset.forName("UTF-8").encode(myString);

If you want to do so at IDE level then you can do:

Window > Preferences > General > Content Types, set UTF-8 as the default encoding for all content types.

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62