我看了孙鑫的Java讲座,有一道程序运行情况如下:代码附后
运行情况:(jdk1.4.2 和jdk1.5都一样)
-------
维新q //输入值
维新 //在孙鑫的视频讲座中运行结果为乱码
4
?? //在孙鑫的视频讲座中运行结果为正常
————import java.util.*;
import java.nio.charset.*;
class CharsetTest
{
public static void main(String[] args) throws Exception
{
Properties pps=System.getProperties();
pps.put("file.encoding","ISO-8859-1");
int data;
byte[] buf=new byte[100];
int i=0;
while((data=System.in.read())!='q')
{
buf[i]=(byte)data;
i++;
}
String str=new String(buf,0,i);
System.out.println(str);//讲座中打印是乱码,可是在我机子上正常显示。
System.out.println(i);
String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");
System.out.println(strGBK);//讲座中打印正常,可是在我机子上是“??”。
}
}
运行情况:(jdk1.4.2 和jdk1.5都一样)
-------
维新q //输入值
维新 //在孙鑫的视频讲座中运行结果为乱码
4
?? //在孙鑫的视频讲座中运行结果为正常
————import java.util.*;
import java.nio.charset.*;
class CharsetTest
{
public static void main(String[] args) throws Exception
{
Properties pps=System.getProperties();
pps.put("file.encoding","ISO-8859-1");
int data;
byte[] buf=new byte[100];
int i=0;
while((data=System.in.read())!='q')
{
buf[i]=(byte)data;
i++;
}
String str=new String(buf,0,i);
System.out.println(str);//讲座中打印是乱码,可是在我机子上正常显示。
System.out.println(i);
String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");
System.out.println(strGBK);//讲座中打印正常,可是在我机子上是“??”。
}
}
没办法,不管设置编码是
gbk还是iso-8859-1都和你的结果是一样的
如下:import java.util.*;
import java.nio.charset.*;
import java.io.*;
class CharsetTest
{
public static void main(String[] args) throws Exception
{
Properties pps=System.getProperties();
pps.put("file.encoding","ISO-8859-1");
int data;
byte[] buf=new byte[100];
int i=0;
while((data=System.in.read())!='q')
{
buf[i]=(byte)data;
i++;
}
String str=new String(buf,0,i);
System.out.println(str);//讲座中打印是乱码,可是在我机子上正常显示。
System.out.println(i);
//String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");
String strGBK=new String(str.getBytes("gb2312"),"GBK");
System.out.println(strGBK);//讲座中打印正常,可是在我机子上是“??”。
}
}顺便给你发点关于java中文问题的质料:
System.out.println(str);
System.out.println(i);
String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");
System.out.println(strGBK);改成这样就和讲座中一样了
Email:[email protected]
Properties pps=System.getProperties();
//pps.list(System.out);
//pps.put("file.encoding","ISO-8859-1");
System.out.println(pps.get("file.encoding"));
int data;
byte[] buf=new byte[100];
int i=0;
while((data=System.in.read())!='q')
{
buf[i]=(byte)data;
i++;
}
//String str=new String(buf,0,i); 有问题,和讲座不一样
String str=new String(buf,"iso-8859-1");//和讲座的一样
System.out.println(str);
System.out.println(i);
String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");
System.out.println(strGBK);
不用ISO-8859-1就可以了,编码和解码最好用同一种编码格式。