Search code examples
javautf-8character-encodingappletarabic

Trying to show arabic characters in Java


I am trying to show arabic characters in a Java applet but I always get Questions marks '?????'.

I tried many solutions with no success:

I am using Windows 7 in a spanish language environment.

Some solutions work when running Netbeans, but they do not work outside this environment. Here it is Netbeans project with sources and .jar.

This is simple code I am using:

package javaapplication4;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

public class JavaApplication4 extends JApplet{

@Override
public void init(){
    try {

        String str1 = new String("تعطي يونيكود رقما فريدا لكل حرف".getBytes(), "UTF-8");
        JOptionPane.showMessageDialog(rootPane, str1);

        String str2 = new String("تعطي يونيكود رقما فر");  
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        os.write(str2.getBytes());
        JOptionPane.showMessageDialog(rootPane, os.toString("UTF-8"));

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(rootPane, ex.toString());
    }
}
}

Any idea of what is happening?


Solution

  • My original Answer is wrong: getBytes() produces a bytearray using the system's default encoding, which netbeans sets to UTF-8.

    Correct answer: Do not use ByteArrayOutputStream and new String(byte[], Charset) at all. Only use Strings. Should work fine.

    EDIT: See comments for the actual problem and explanation why solution is not completely possible.