public String getChineseCharAll() {
創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供成都服務(wù)器托管 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。
// 獲取第一個(gè)漢字的16進(jìn)制
String start = "4e00";
// 獲取最后一個(gè)漢字的16進(jìn)制
String end = "9fa5";
// 將字符串變?yōu)槭M(jìn)制整數(shù)
int s = Integer.parseInt(start, 16);
int e = Integer.parseInt(end, 16);
// 創(chuàng)建字符串緩沖區(qū),因?yàn)閱尉€程,所以用StringBuilder提高效率
StringBuilder sb = new StringBuilder();
for(int i = s, count = 1; i = e; i++, count++) {
// 每50個(gè)漢字進(jìn)行換行輸出
if(count % 50 == 0) {
sb.append((char) i + "\n");
} else {
sb.append((char) i + " ");
}
}
return new String(sb);
}
public class Demo {public static void main(String args[]){for(int i=0;i=65535;i++){System.out.print((char)i+" ");if(i%10==0)System.out.println();}}} 這個(gè)程序可以輸出所有的字符,包括漢字,字母,日語等國外字符。
public?class?Test?{
public?static?void?main(String[]?args)?{
RandomHan?han?=?new?RandomHan();
System.out.print(han.getRandomHan());
}
}
class?RandomHan?{
private?Random?ran?=?new?Random();
private?final?static?int?delta?=?0x9fa5?-?0x4e00?+?1;
public?char?getRandomHan()?{
return?(char)(0x4e00?+?ran.nextInt(delta));?
}
}
隨機(jī)生成漢字的代碼,希望能幫助到您。
import java.util.Random;
public class Demo {
public static void main(String[] args) {
String[] str ={"一","二","三","四","五","六","七","八","九","十"};
//演示漢字,里面的漢字自己更改
Random r= new Random();
int i = r.nextInt(10);
System.out.println("隨機(jī)產(chǎn)生的第"+(i+1)+"位數(shù):"+str[i]);
}
}
public static void main(String[] args) {
Random random = new Random();
String dic = "你我他帥酷衰";
String msg = "";
for (int i = 0; i 10; i++) {
int temp = random.nextInt(dic.length());
msg += dic.charAt(temp);
}
System.out.println(msg);
}
就是用隨機(jī)數(shù)控制輸出第幾個(gè)漢字:
output:
帥我我他衰他酷他衰帥
/**
* 原理是從漢字區(qū)位碼找到漢字。在漢字區(qū)位碼中分高位與底位, 且其中簡體又有繁體。位數(shù)越前生成的漢字繁體的機(jī)率越大。
* 所以在本例中高位從171取,底位從161取, 去掉大部分的繁體和生僻字。但仍然會(huì)有??!
*
*/
@Test
public void create() throws Exception {
String str = null;
int hightPos, lowPos; // 定義高低位
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39)));//獲取高位值
lowPos = (161 + Math.abs(random.nextInt(93)));//獲取低位值
byte[] b = new byte[2];
b[0] = (new Integer(hightPos).byteValue());
b[1] = (new Integer(lowPos).byteValue());
str = new String(b, "GBk");//轉(zhuǎn)成中文
System.err.println(str);
}
/**
* 旋轉(zhuǎn)和縮放文字
* 必須要使用Graphics2d類
*/
public void trans(HttpServletRequest req, HttpServletResponse resp) throws Exception{
int width=88;
int height=22;
BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(new Font("黑體",Font.BOLD,17));
Random r = new Random();
for(int i=0;i4;i++){
String str = ""+r.nextInt(10);
AffineTransform aff = new AffineTransform();
aff.rotate(Math.random(),i*18,height-5);
aff.scale(0.6+Math.random(), 0.6+Math.random());
g2d.setTransform(aff);
g2d.drawString(str,i*18,height-5);
System.err.println(":"+str);
}
g2d.dispose();
ImageIO.write(img, "JPEG",resp.getOutputStream());
}