import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class CharsetTest { /**
 * @param args
 */
public static void main(String[] args) throws Exception {
Properties pps = System.getProperties();
pps.put("file.encoding", "ISO-8859-1");
pps.list(System.out);
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);
 
        
}}
如上程序中使用pps.put("file.encoding", "ISO-8859-1");将JAVA的编码方式设置成ISO-8859-1,但是为什么我输入汉字时还能正常显示呢?
另外在eclipse中“Run Configurations”中选择“Common”页选项“Other”中选“ISO-8859-1”,执行程序时,输入汉字就不能正常显示了,这是为什么呢?

解决方案 »

  1.   

    System.setProperties(),这样更改系统属性应该是不管用的,在运行到你代码哪里时,虚拟机早已启动了。试试在运行参数中加 -Dfile.encoding=ISO-8859-1试试。
      

  2.   

    在eclipse中不是加在 program parameters 那里,是VM parameters
      

  3.   


    楼主这个说明你的“ pps.put("file.encoding", "ISO-8859-1");”并没有改变系统的编码。
    这样从外部读入的字符都会按照系统编码的方式进行组织。用的第二种方式与这句“pps.put("file.encoding", "ISO-8859-1")”没有关系。因此改变字符编码的方式不是“ pps.put("file.encoding", "ISO-8859-1")”所能处理的。