我有一个简单的ReadWriteUnicode类,它使用drawString在Frame中显示unicode值。我使用\ u65e5 \ u672和MONOSPACED字体测试它并获得中文。当我传递\ ue384b1 \ ue384b2等字体名称Hangul时,我得到[] b1 [] b2,其中[]是一个方框。
如何获得汉字韩语的三字节代码?
public class ReadWriteUnicodeExample {
static void writeOutput(String str) {
try {
FileOutputStream fos = new FileOutputStream("test.txt");
Writer out = new OutputStreamWriter(fos, "UTF8");
out.write(str);
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
static String readInput() {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char)ch);
}
in.close();
return buffer.toString();
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String jaString = new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");
writeOutput(jaString);
String inputString = readInput();
String displayString = jaString + " " + inputString;
new ShowString(displayString, "Conversion Demo", "MONOSPACED");
String koreanStr = new String ("\ue384b1\ue384b2\ue384b4\ue384b7\ue384b8\ue384b9");
writeOutput(koreanStr);
inputString = readInput();
displayString = koreanStr + " " + inputString;
new ShowString(displayString, "Conversion Demo", "Hangul");
}
}
class ShowString extends Frame {
FontMetrics fontM;
String outString;
ShowString (String target, String title, String fontName) {
setTitle(title);
outString = target;
Font font = new Font(fontName, Font.PLAIN, 36);
fontM = getFontMetrics(font);
setFont(font);
int size = 0;
for (int i = 0; i < outString.length(); i++) {
size += fontM.charWidth(outString.charAt(i));
}
size += 24;
setSize(size, fontM.getHeight() + 60);
setLocation(getSize().width/2, getSize().height/2);
setVisible(true);
}
public void paint(Graphics g) {
Insets insets = getInsets();
int x = insets.left;
int y = insets.top;
g.drawString(outString, x + 6, y + fontM.getAscent() + 14);
}
}