java 编码,乱码,字符系列(2)

本文深入探讨了Java中不同编码方式的处理方法,包括文件读取时的编码转换问题及其解决方案。通过实例演示了如何避免因编码不匹配导致的乱码现象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java编码方式及处理方法介绍

按照步骤运行下面代码:

System.out.println(Charset.defaultCharset());
 

所输出结果均已本地测试为准。不同操作系统可能输出结果不同

执行上述代码 :GBK   表明系统默认的编码方式为GBK

     打开记事本,新建一个文件。取名为 gbk.txt;之所以取名为gbk,是因为该文件是按照系统默认的编码方式编码,及GBK,文件内容为  :挨踢good

 

执行下述代码:

// gbk.txt use the default character encoding
		InputStream is1 = new FileInputStream("gbk.txt");
		InputStreamReader streamReader1 = new InputStreamReader(is1);
		char[] chars1 = new char[is1.available()];
		streamReader1.read(chars1, 0, is1.available());
		System.out.println(new String(chars1));

    输出结果:挨踢  bad

执行下述代码:

System.out.println("------------utf----------------");
		InputStream is = new FileInputStream("gbk.txt");
		InputStreamReader streamReader = new InputStreamReader(is, "utf-8");
		System.out.println("change encoding " + streamReader.getEncoding());
		char[] chars = new char[is.available()];
		streamReader.read(chars, 0, is.available());
		System.out.println(new String(chars));

  输出结果:------------utf----------------
                    change encoding UTF8
                    ????good

解释:在此处,当我们使用utf-8 编码方式读取文件gbk.txt时,文件出现乱码。是因为我们是用系统默认的“gbk”编码方式创建的文件。当用utf-8读取时,文件肯定出错。

 2,为什么 good 输出没有乱码?这与具体的编码方式有关,对于英文字母的处理,gbk 和utf-8采取的编码方式可以看做是没有出入的。具体可参http://alan0509.iteye.com/blog/729929

执行下述代码:

 

创建文件,utf8.txt,很明显,采用的编码方式是utf-8。文件内容为:上海世大咨询(在此,我没有加入英文。)

执行下面代码:

		System.out.println("------------utf----------------");
		InputStream is2 = new FileInputStream("utf8.txt");
		InputStreamReader streamReader2 = new InputStreamReader(is2);
		System.out.println("use gbk to read utf-8 "
				+ streamReader2.getEncoding());
		char[] chars2 = new char[is2.available()];
		streamReader2.read(chars2, 0, is2.available());
		System.out.println(new String(chars2) + chars2.length);

  输出结果:

------------utf----------------
use gbk to read utf-8 GBK
锘夸笂娴蜂笘澶у挩璇 21

可见,当我们读取一个文件时,如果不指定编码方式,将自动采用“系统默认的方式读取”。至此,我们发现第一个乱码问题:当我们读取文件时,有必要知道文件的编码方式。

     执行下面代码:

System.out.println("-----------use byte-----------------");
		File file2 = new File("utf8.txt");
		InputStream inputStream = new FileInputStream(file2);
		byte[] bs = new byte[(int) file2.length()];
		inputStream.read(bs);

		String s = new String(bs, "utf-16");
		System.out.print("use byte to read utf-8,and use utf-16 decode"
				+ "use utf-16  " + s);
		System.out.println();

		String s2 = new String(bs, "utf-8");
		System.out.print("use byte read utf-8,and utf-8 decode" + "use utf-8  "
				+ s2);
		System.out.println();

  输出结果:

-----------use byte-----------------
use byte to read utf-8,and use utf-16 decodeuse utf-16  ??????銨??
use byte read utf-8,and utf-8 decodeuse utf-8  ?上海世大咨询

结果解释:此处,我采取的方式是将文件中的内容以字节方式读取,然后针对字节进行编码。当我采用utf-16编码时并不能还原文件。当采用utf-8编码时能还原文件。但此处多出一个问号。具体原因将在后面文章中介绍。

执行下面代码:

System.out.println("------------utf----------------");
		File file = new File("utf8.txt");
		InputStream is3 = new FileInputStream(file);
		InputStreamReader streamReader3 = new InputStreamReader(is3, "utf-8");
		System.out.println(streamReader3.getEncoding());
		char[] chars3 = new char[is3.available()];
		streamReader3.read(chars3, 0, is3.available());
		System.out.println(new String(chars3));

  输出结果:

 ------------utf----------------
UTF8
?上海世大咨询                    注:问号问题将在后面介绍。

执行下面代码:

System.out.println("------------------------");
		String sp = "上海世大";
		// use default encodeing to get bytes
		byte[] bs2 = sp.getBytes();
		byte[] bs3 = sp.getBytes("utf-8");
		String string = new String(bs3, "utf-8");
		String string2=new String(bs2,Charset.defaultCharset());
		String string3 = new String(bs2, "utf-16");
		System.out
				.println("use default character encodeing,and then use utf-8 to decode    "
						+ string);
		System.out
		.println("use default character encodeing,and then use gbk to decode    "
				+ string2);
		System.out
		.println("use default character encodeing,and then use utf-16 to decode    "
				+ string3);

  输出结果:

 ------------------------
use default character encodeing,and then use utf-8 to decode    上海世大
use default character encodeing,and then use gbk to decode    上海世大
use default character encodeing,and then use utf-16 to decode    ????

可见当我们使用getbytes方法时,指定具体编码方式是有好处的。

 

对java处理编码的一点补充:

  • Java的class文件采用utf8的编码方式,JVM运行时采用utf16。(注意是java的class文件。.java文件并不是class文件,我们写的java文件在没有指定编码的情况下,将根据IDE工具指定的编码方式。一般情况下会采用系统默认的编码方式,所以我们的java文件拿到不同的机器上运行时可能出现乱码,具体原因上面文章有介绍。)
  • 请看我的第三篇文章。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值