看这段代码
int a[] = { 0x2A6B2 };
String s = new String(a, 0, a.length);
System.out.println(s);0x2A6B2 是一个增补字符,但这段代码显示结果啥也看不到
int a[] = { 0x2A6B2 };
String s = new String(a, 0, a.length);
System.out.println(s);0x2A6B2 是一个增补字符,但这段代码显示结果啥也看不到
Unicode 增补字符,这里使用的是“方正超大字符集”。import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Random;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;import javax.imageio.ImageIO;public class Test { public static void main(String[] args) throws Exception {
int a[] = { 0x2A6B1 };
String str = new String(a, 0, a.length); int width = 120, height = 120;
int size = 100;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.fillRect(0, 0, width, height);
g.setFont(new Font("宋体-方正超大字符集", Font.PLAIN, size));
g.setColor(Color.BLACK);
g.drawString(str, 10, 90);
g.dispose(); File file = new File("2A6B1.png");
OutputStream os = new FileOutputStream(file);
ImageIO.write(image, "png", os);
os.close();
System.out.println("OK");
}
}正因为使用 2A6B1 字符,是由于“方正超大字符集”中没有定义 2A6B2 这个字符,输出的图片如下:Unicode 标准的字符是:
StringBuilder sb = new StringBuilder((Character.MAX_CODE_POINT - Character.MAX_VALUE) * 2 + 9);
for (int i = Character.MAX_VALUE; i <= Character.MAX_CODE_POINT; ++i) {
sb.appendCodePoint(i);
}
String s = sb.toString();
String filepath = "C:/test.txt";
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(filepath), "utf-8");
osw.write(s);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
osw = null;
}
}
}
}}
执行上面代码,将生成的文件使用任意文本编辑器以 UTF-8 编码打开,如果系统有支持的字体,应该能看到所有的 Unicode 增补字符。