How to convert Chinese character to Unicode ?


I'm trying to convert an English version web based application into Chinese version using Java and Oracle as database. Can any one tell me how to convert the Chinese character to unicode in order to store inside the database and retrieve it from the database and convert it back to Chinese character and display it through browser ?  
Who is Participating?
vladi21Connect With a Mentor Commented:

Loading GB or Big5 files

Java comes with a classes called InputStreamReader and OutputStreamWriter that translate into and out of Unicode from local encodings. Fortunately, two of the supported encodings are GB2312 (used in mainland China and Singapore) and Big5 (used in Hong Kong and Taiwan). Below is a sample program that converts a GB2312 file to UTF-8. It is derived from a sample program in Java in a Nutshell.

public class inputtest {
  public static void main(String[] args) {
    String outfile = null;

    try { convert(args[0], args[1], "GB2312", "UTF8"); } // or "BIG5"
    catch (Exception e) {

  public static void convert(String infile, String outfile, String from, String to)
       throws IOException, UnsupportedEncodingException
    // set up byte streams
    InputStream in;
    if (infile != null) in = new FileInputStream(infile);
    else in =;
    OutputStream out;
    if (outfile != null) out = new FileOutputStream(outfile);
    else out = System.out;

    // Use default encoding if no encoding is specified.
    if (from == null) from = System.getProperty("file.encoding");
    if (to == null) to = System.getProperty("file.encoding");

    // Set up character stream
    Reader r = new BufferedReader(new InputStreamReader(in, from));
    Writer w = new BufferedWriter(new OutputStreamWriter(out, to));

    // Copy characters from input to output.  The InputStreamReader
    // converts from the input encoding to Unicode,, and the OutputStreamWriter
    // converts from Unicode to the output encoding.  Characters that cannot be
    // represented in the output encoding are output as '?'
    char[] buffer = new char[4096];
    int len;
    while((len = != -1)
      w.write(buffer, 0, len);


Displaying Chinese

Finding Chinese Fonts

Java 2 allows the programmer to directly access the fonts on the machine. The code sample below gets a list of all the fonts on the system, and then checks each font to see if it can display a sample Chinese string. Matching fonts are printed. Variations of the below code can be used to automatically find Chinese fonts and set the font of the Swing components accordingly.
fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
System.out.println("Fonts that support Chinese: \n");
for (int i = 0; i < fonts.length; i++) {
    if (fonts[i].canDisplayUpTo("\u4e00") > 0) {


Previous to the introduction of Swing set of peerless Java AWT components, Java could not display Chinese except on Chinese operating systems. With Swing, you can display Chinese in any component, providing you have fonts that support Chinese on your system. Previously, to find a font that could display Chinese you needed to modify a file called to list the available Chinese fonts. This is not a simple process for the average user and I think the above code is easier to use. But in case you do need to modify, here is an excerpt from my file, where Bitstream Cyberbit is the Unicode font. A list of Unicode fonts supporting Chinese can be found here.
dialog.3=Bitstream Cyberbit

dialoginput.0=Courier New,ANSI_CHARSET
dialoginput.3=Bitstream Cyberbit

serif.0=Times New Roman,ANSI_CHARSET
serif.3=Bitstream Cyberbit

sansserif.3=Bitstream Cyberbit

monospaced.0=Courier New,ANSI_CHARSET
monospaced.3=Bitstream Cyberbit
Java 1.2 is supposed to come with Unicode fonts included. Unfortunately, these fonts do not support Chinese, Japanese, or Korean yet.

Chinese and Swing

Swing components can display any Unicode character that you have the font for. Here is a sample program that reads a GB file and displays it in a JTextArea.
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

public class swingsample extends JFrame {
    private static JTextArea mTextArea;

    public swingsample(String filename) {
super("GB File Viewer");
try {
    loadfile(filename, "GB2312");  // or "BIG5"
catch (Exception loadexc) {

    public static void loadfile(String filename, String enc)
throws IOException, UnsupportedEncodingException
String newline;
String buffer;
InputStream in;

newline = System.getProperty("line.separator");

in = new FileInputStream(filename);
// Set up character stream
BufferedReader r = new BufferedReader(new InputStreamReader(in, enc));
while ((buffer = r.readLine()) != null) {
    mTextArea.append(buffer + newline);

    protected void createUI() {
setSize(500, 500);
Container content = getContentPane();
content.setLayout(new BorderLayout());

mTextArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(mTextArea,
content.add(scrollPane, BorderLayout.CENTER);

// Exit the application when the window is closed.
addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {

    public static void main(String[] args) {
new swingsample(args[0]);
} // swingsample

andricAuthor Commented:
Thanks !
All Courses

From novice to tech pro — start learning today.