请教一个关于java编码的问题!
想问各位一个关于JAVA文件的编码问题:
Java的源文件是以何种方式存放的,比如在ISO-8859-1编码的操作系统,如Unix中和在以GBK为默认编码方式的操作系统,如中文的WIN2000中,JAVA的源文件中的字符串String str="Hello world 世界你好!"中的汉字"世界你好"是以什么样的编码存放的? 如果在英文操作系统下打印出来会是什么效果?
System.out.println(System.getProperty("file.encoding") + "\nstring=" + hello);其中英文操作系统的默认编码方式是ISO-8859-1
System.out.println(">>>>testing1: write hello world to files<<<<");
System.out.println("[test 1-1]: with system default encoding="
+ System.getProperty("file.encoding") + "\nstring=" + hello
+ "\tlength=" + hello.length());
printCharArray(hello);
writeFile("hello.orig.html", hello);
其中printCharArray函数如下:
public static void printCharArray(String inStr) {
char[] myBuffer = inStr.toCharArray();
//list each Charactor in byte value, short value, and UnicodeBlock Mapping
for (int i = 0; i < inStr.length(); i++) {
byte b = (byte) myBuffer[i];
short s = (short) myBuffer[i];
String hexB = Integer.toHexString(b).toUpperCase();
String hexS = Integer.toHexString(s).toUpperCase();
StringBuffer sb = new StringBuffer();
//print char
sb.append("char[");
sb.append(i);
sb.append("]='");
sb.append(myBuffer[i]);
sb.append("'\t");
//byte value
sb.append("byte=");
sb.append(b);
sb.append(" \\u");
sb.append(hexB);
sb.append('\t');
//short value
sb.append("short=");
sb.append(s);
sb.append(" \\u");
sb.append(hexS);
sb.append('\t');
//Unicode Block
sb.append(Character.UnicodeBlock.of(myBuffer[i]));
System.out.println(sb.toString());
}
System.out.println();
}
这是我在一篇网上看的文章中看到的下面的代码的执行结果:
========testing1: write hello world to files========[test 1-1]: with system default encoding=ISO-8859-1string=Hello world 世界你好 length=20char[0]='H' byte=72 \u48 short=72 \u48 BASIC_LATINchar[1]='e' byte=101 \u65 short=101 \u65 BASIC_LATINchar[2]='l' byte=108 \u6C short=108 \u6C BASIC_LATINchar[3]='l' byte=108 \u6C short=108 \u6C BASIC_LATINchar[4]='o' byte=111 \u6F short=111 \u6F BASIC_LATINchar[5]=' ' byte=32 \u20 short=32 \u20 BASIC_LATINchar[6]='w' byte=119 \u77 short=119 \u77 BASIC_LATINchar[7]='o' byte=111 \u6F short=111 \u6F BASIC_LATINchar[8]='r' byte=114 \u72 short=114 \u72 BASIC_LATINchar[9]='l' byte=108 \u6C short=108 \u6C BASIC_LATINchar[10]='d' byte=100 \u64 short=100 \u64 BASIC_LATINchar[11]=' ' byte=32 \u20 short=32 \u20 BASIC_LATINchar[12]='? byte=-54 \uFFFFFFCA short=202 \uCA LATIN_1_SUPPLEMENTchar[13]='? byte=-64 \uFFFFFFC0 short=192 \uC0 LATIN_1_SUPPLEMENTchar[14]='? byte=-67 \uFFFFFFBD short=189 \uBD LATIN_1_SUPPLEMENTchar[15]='? byte=-25 \uFFFFFFE7 short=231 \uE7 LATIN_1_SUPPLEMENTchar[16]='? byte=-60 \uFFFFFFC4 short=196 \uC4 LATIN_1_SUPPLEMENTchar[17]='? byte=-29 \uFFFFFFE3 short=227 \uE3 LATIN_1_SUPPLEMENTchar[18]='? byte=-70 \uFFFFFFBA short=186 \uBA LATIN_1_SUPPLEMENTchar[19]='? byte=-61 \uFFFFFFC3 short=195 \uC3 LATIN_1_SUPPLEMENT第1步:在英文编码环境下,虽然屏幕上正确的显示了中文,但实际上它打印的是“半个”汉字,将结果写入第1个文件 hello.orig.html
我不知道那名:"在英文编码环境下,虽然屏幕上正确的显示了中文,但实际上它打印的是“半个”汉字"这句话是什么意思?在英文编码环境下能打印出中文来???
上述的代码从编码到打印究竟经过了哪几个层次的转换?请高手指点啊!~~~~~~谢谢~!~~~~~