写了一段代码,实现从web服务器中读出一段UTF8编码的文件
相干代码:
while ((str = urlReader.readLine()) != null) {
//读取str
}
resultText.setText(new String(str.toString().getBytes(),
"UTF8"));注:resultText为SWT中的Text,str是从web服务器中读出来的数据流,编码为UTF-8/*******************/同一文件内的英文没有问题,可是中文却乱码,而且多出在标点符号后。等待高人解围~~错误实例:(resultText中复制过来的)??般来??,压缩档案不应包含??有档案压缩目录下,例如 Java语言文字档案和档案卷宗应排除.
相干代码:
while ((str = urlReader.readLine()) != null) {
//读取str
}
resultText.setText(new String(str.toString().getBytes(),
"UTF8"));注:resultText为SWT中的Text,str是从web服务器中读出来的数据流,编码为UTF-8/*******************/同一文件内的英文没有问题,可是中文却乱码,而且多出在标点符号后。等待高人解围~~错误实例:(resultText中复制过来的)??般来??,压缩档案不应包含??有档案压缩目录下,例如 Java语言文字档案和档案卷宗应排除.
这个不行~那就不是只有几个乱码的问题了,基本上没有 不乱码的了做一下字符转换应该可以!到网上找找
标准库函数里有的,为什么去网上找?谢谢~
"UTF8"));试下.
"UTF-8"));
* Parameters are as in System.arraycopy
* Return first index in `dst' past the last copied char.
* @param src The array holding the bytes to convert.
* @param sindex The start index from which bytes are converted.
* @param dst The array holding the converted characters..
* @param dindex The start index from which converted characters
* are written.
* @param len The maximum number of bytes to convert.
*/
public static int utf2chars(byte[] src, int sindex,
char[] dst, int dindex,
int len) {
int i = sindex;
int j = dindex;
int limit = sindex + len;
while (i < limit) {
int b = src[i++] & 0xFF;
if (b >= 0xE0) {
b = (b & 0x0F) << 12;
b = b | (src[i++] & 0x3F) << 6;
b = b | (src[i++] & 0x3F);
} else if (b >= 0xC0) {
b = (b & 0x1F) << 6;
b = b | (src[i++] & 0x3F);
}
dst[j++] = (char)b;
}
return j;
} /** Return bytes in Utf8 representation as an array of characters.
* @param src The array holding the bytes.
* @param sindex The start index from which bytes are converted.
* @param len The maximum number of bytes to convert.
*/
public static char[] utf2chars(byte[] src, int sindex, int len) {
char[] dst = new char[len];
int len1 = utf2chars(src, sindex, dst, 0, len);
char[] result = new char[len1];
System.arraycopy(dst, 0, result, 0, len1);
return result;
} /** Return all bytes of a given array in Utf8 representation
* as an array of characters.
* @param src The array holding the bytes.
*/
public static char[] utf2chars(byte[] src) {
return utf2chars(src, 0, src.length);
} /** Return bytes in Utf8 representation as a string.
* @param src The array holding the bytes.
* @param sindex The start index from which bytes are converted.
* @param len The maximum number of bytes to convert.
*/
public static String utf2string(byte[] src, int sindex, int len) {
char dst[] = new char[len];
int len1 = utf2chars(src, sindex, dst, 0, len);
return new String(dst, 0, len1);
} /** Return all bytes of a given array in Utf8 representation
* as a string.
* @param src The array holding the bytes.
*/
public static String utf2string(byte[] src) {
return utf2string(src, 0, src.length);
}
只能说,“web服务器在发送数据流的时候采用的是 UTF-8 编码”,而经过 urlReader.readLine() 得到的 str 是一个 Java 的 String 对象,是没有“编码”一说的。
java内部使用unicode,那么这个String对象也应该是unicode保存的吧~
1 确认你读取的文件的编码方式
2 确认你显示平台上支持的编码方式
比如源文件的编码方式GB2312,需要在支持UTF-8的平台上显示,步骤如下
char gb2312Data = s.getBytes("GB2312");
String newString = new String(gb2312Data,"UTF-8");