Java IO操作——字符编码

本文介绍了ISO8859-1、GBK/GB2312、Unicode和UTF等常用编码的特点,并深入探讨了乱码产生的原因及解决方法。

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

学习目标

了解常用的几种编码
掌握乱码的产生原因

编码:
在计算机的世界里,任何的文字都是以指定的编码方式存在的,在JAVA程序的开发中最常见的是以下的几种编码:
ISO8859-1、GBK/GB2312、unicode、UTF
iso-8859-1编码属于单字节编码,最多只能表示0~255的字符范围,主要在英文上应用。
GBK/GB2312:中文的国标编码,专门用来表示汉字,是双字节编码。
unicode:Java中就是使用此编码方式,也是最标准的一种编码,是使用16进制表示的编码。但此编码不兼容iso8859-1编码。
UTF:由于unicode不支持iso8859-1编码,而且容易占用更多的空间,而且对于英文字母也需要使用两个字节编码,这样使用unicode不便于传输和存储,因此产生了utf编码,utf兼容iso8859-1编码,同时也可以用来表示所有的语言字符,不过utf编码是不定长编码,每一个字符的长度从1~6个字节不等,一般在中文网页中使用此编码,因为这样可以节省空间。

乱码的产生
在程序中如果处理不好字符的编码,则就有可能出现乱码问题,如果现在本机的默认编码是GBK,但在程序中使用了iso-8859-1编码,则就会出现字符的乱码情况,就好比两个人交谈,一个人说的是中文,另外一个人说的是其他语言,如果语言不同,则肯定无法沟通。
如果没有处理好编码的问题,则肯定在程序中出现乱码。

得到本机的编码显示:
使用System类可以取得与系统有关的信息,那么直接使用此类就可以找到系统的默认编码,使用如下方法:
public static Properties getProperty()
System类可以取得系统的相关信息,一直在接触此类。
public class CharSetDemo01{
	public static void main(String args[]){
		System.out.println("系统默认编码:" +
			System.getProperty("file.encoding")) ;	// 获取当前系统编码
	}
};
因为现在的本机环境,是中文环境,所以是使用GBK进行编码,很明显如果现在在程序中使用了ISO8859-1编码。如果要编码,则需要使用String类的支持。
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
可以指定使用的字符编码。
import java.io.OutputStream ;
import java.io.FileOutputStream ;
import java.io.File ;
public class CharSetDemo02{
	public static void main(String args[]) throws Exception {
		File f = new File("D:" + File.separator + "test.txt") ;	// 实例化File类
		OutputStream out = new FileOutputStream(f) ;	// 实例化输出流
		byte b[] = "中国,你好!".getBytes("ISO8859-1") ;	// 转码操作
		out.write(b) ;	// 保存
		out.close() ;	// 关闭
	}
};
乱码产生的根本原因就是字符编码不统一造成的。

总结:
1、了解几种常用的编码特点。
2、理解乱码是如何产生的。



C#简单变色龙游戏 /// /// 创建一副52张的新牌 /// /// public static System.Collections.ArrayList Creat() { System.Collections.ArrayList list = new System.Collections.ArrayList(); for (int i = 3; i <= 6; i++) { for (int j = 1; j <= 13; j++) { Poker p = new Poker(); switch (j) { case 1: p = new Poker((PokerStyle)i, "A", j, 11); break; case 2: p = new Poker((PokerStyle)i, j.ToString(), j, 10); break; case 11: p = new Poker((PokerStyle)i, "J", j, 11); break; case 12: p = new Poker((PokerStyle)i, "Q", j, 2); break; case 13: p = new Poker((PokerStyle)i, "K", j, 3); break; default: p = new Poker((PokerStyle)i, j.ToString(), j, j); break; } list.Add(p); } } return list; } /// /// 洗牌 /// /// /// public static System.Collections.ArrayList shuffle(System.Collections.ArrayList list)//洗牌 { System.Random r = new Random(); for (int i = 0; i <= 7; i++) { int rightOrRight = r.Next(1, 3); Poker[] leftPoker = new Poker[list.Count / 2]; Poker[] rightPoker = new Poker[list.Count % 2 == 0 ? list.Count / 2 : list.Count / 2 + 1]; list.CopyTo(0, leftPoker, 0, list.Count / 2); list.CopyTo(list.Count / 2, rightPoker, 0, list.Count % 2 == 0 ? list.Count / 2 : list.Count / 2 + 1); if (rightOrRight == 1)//左边插右边 { list.Clear(); for (int j = 0; j <= leftPoker.Length - 1; j++) { list.Add(rightPoker[j]); list.Add(leftPoker[j]); } if (leftPoker.Length != rightPoker.Length) { list.Add(rightPoker[rightPoker.Length - 1]); } } else { list.Clear(); for (int j = 0; j <= leftPoker.Length - 1; j++) { list.Add(leftPoker[j]); list.Add(rightPoker[j]); } if (leftPoker.Length != rightPoker.Length) { list.Add(rightPoker[rightPoker.Length - 1]); } } } return list; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值