I am trying to show arabic characters in a Java applet but I always get Questions marks '?????'.
I tried many solutions with no success:
- Using new String(bytes [], charsetName) to UTF-8 decode.
- Changing default Charset in Netbeans: -Dfile.encoding=UTF8 in VM options and -encoding UTF8 in compiling options.
- Using ByteArrayOutputStream for encoding.
- Using both UTF8 and UTF-8 charset names.
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?
String
is aString
. Encoding is only applicable if you are readingbyte
s from a file and need them to becomechar
s. – Encode