import java.io.*;
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) {
System.out.print(e.getMessage());
System.exit(1);
}
}
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 = System.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 = r.read(buffer)) != -1)
w.write(buffer, 0, len);
r.close();
w.flush();
w.close();
}
}
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) {
System.out.println(fonts[i].getFamily());
}
}
dialog.0=Arial,ANSI_CHARSET dialog.1=WingDings,SYMBOL_CHARSET,NEED_CONVERTED dialog.2=Symbol,SYMBOL_CHARSET,NEED_CONVERTED dialog.3=Bitstream Cyberbit dialoginput.0=Courier New,ANSI_CHARSET dialoginput.1=WingDings,SYMBOL_CHARSET,NEED_CONVERTED dialoginput.2=Symbol,SYMBOL_CHARSET,NEED_CONVERTED dialoginput.3=Bitstream Cyberbit serif.0=Times New Roman,ANSI_CHARSET serif.1=WingDings,SYMBOL_CHARSET,NEED_CONVERTED serif.2=Symbol,SYMBOL_CHARSET,NEED_CONVERTED serif.3=Bitstream Cyberbit sansserif.0=Arial,ANSI_CHARSET sansserif.1=WingDings,SYMBOL_CHARSET,NEED_CONVERTED sansserif.2=Symbol,SYMBOL_CHARSET,NEED_CONVERTED sansserif.3=Bitstream Cyberbit monospaced.0=Courier New,ANSI_CHARSET monospaced.1=WingDings,SYMBOL_CHARSET,NEED_CONVERTED monospaced.2=Symbol,SYMBOL_CHARSET,NEED_CONVERTED 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.
import java.lang.*;
import java.io.*;
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");
createUI();
try {
loadfile(filename, "GB2312"); // or "BIG5"
}
catch (Exception loadexc) {
}
setVisible(true);
}
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);
}
r.close();
}
protected void createUI() {
setSize(500, 500);
Container content = getContentPane();
content.setLayout(new BorderLayout());
mTextArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(mTextArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
content.add(scrollPane, BorderLayout.CENTER);
// Exit the application when the window is closed.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new swingsample(args[0]);
}
} // swingsample
Editor's Note: I'm still learning about the InputMethod classes and will include more information as available.