IO流七:字符编码、转换流的字符编码、编码与解码、IO流练习
2014-03-24 15:18
459 查看
1 字符编码表介绍、转换流的字符编码
字符流的出现为了方便操作字符。更重要是的加入了编码转换。
通过子类转换流来完成。
InputStreamReader
OutputStreamWriter
在两个对象进行构造的时候可以加入字符集。
编码表的由来:
计算机只能识别二进制数据,早期由来是电信号。
为了方便应用计算机,让它可以识别各个国家的文字。
就将各个国家的文字用数字来表示,并一一对应,形成一张表。
这就是编码表。
常见的编码表:
ASCII:美国标准信息交换码。用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表用一个字节的8位表示。
GB2312:中国的中文编码表,一个汉字用2个字节表示。
GBK:中国的中文编码表升级,融合了更多的中文文字符号。
Unicode:国际标准码,融合了多种文字。所有文字都用两个字节来表示,Java语言使用的就是unicode。
UTF-8:最多用三个字节来表示一个字符。
转换流的编码应用:
可以将字符以指定编码格式存储。
可以对文本数据指定编码格式来解读。
指定编码表的动作由构造函数完成。
字符编码:
编码:字符串-->字节数组
解码:字节数组-->字符串
通过转换流指定字符编码表代码示例:
import java.io.*; class EncodeStream{ public static void main(String[] args) throws IOException{ writeText(); readTest(); } //写操作中的字符编码。 public static void writeText() throws IOException{ OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("e:\\encode.txt"),"UTF-8"); osw.write("你好"); osw.close(); } //读操作中的字符解码。 public static void readTest() throws IOException{ InputStreamReader isr = new InputStreamReader(new FileInputStream("e:\\encode.txt"),"UTF-8"); char[] buf = new char[10]; int len = isr.read(buf); String str = new String(buf,0,len); System.out.println(str); isr.close(); } }
2 编码和解码
编码:字符串变成字节数组。解码:字节数组变成字符串。
编码 String-->byte[]: str.getBytes();
解码 byte[]-->String: new String(byte[]);
代码示例:
import java.util.*; class EncodeDemo { public static void main(String[] args) throws Exception{ String s = "你好"; byte[] b1 = s.getBytes("GBK"); //对字符串s用GBK编码,字符串变字节数组。 System.out.println(Arrays.toString(b1)); String s1 = new String(b1,"ISO8859-1"); //对字节数组b1解码,字符数组变字符串。 System.out.println("s1="+s1); //对字符串s1进行ISO8859-1编码。 byte[] b2 = s1.getBytes("ISO8859-1"); System.out.println(Arrays.toString(b2)); String s2 = new String(b2,"GBK"); //对字节数组b2以GBK码表解码。 System.out.println("s2="+s2); /* 如果把上面代码中的"ISO8859-1"换成"UTF-8",则最终不能得到“你好”, 因为,第二次编码时得到的字节数组不是第一次编码时的字节数组, 这是因为GBK和UTF-8都识别中文造成的。*/ } }
3 字符编码小示例:联通
现象:新建一个txt记事本,输入“联通”两字,保存关闭后再打开,发现是乱码。“联通”根据GBK码表编码后,得到的的字节数组中有四个字节,(GBK码表中一个汉字用2个字节表示)
这四个字节的有效位分别是 11000001、10101010、11001101、10101001.
恰巧符合UTF-8的编码规则,所以再次打开时,会以UTF-8码表进行解码,就会变成乱码。
附UTF-8编码规则:
UTF-8规定,一个汉字最少用一个字节,最多用3个字节表示。
一个字节表示一个汉字时,首位以0开头: 0xxxxxxx
两个字节表示一个汉字时,第一个字节以110开头,第二个字节以10开头: 110xxxxx 10xxxxxx
三个字节表示一个汉字时,第一个自己以1110开头,后两个字节以10开头: 1110xxxx 10xxxxxx 10xxxxxx
4 IO流学习后的练习
需求:有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式(如:zhangsan,30,40,60),计算出总成绩。
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件“stud.txt”中。
1,描述学生对象。
2,定义一个可操作学生对象的工具类。
思路:
1,通过获取键盘录入一行数据,并将该行中的信息取出封
装成学生对象。
2,因为学生对象有很多,那么就需要存储,使用到集合。因为要对学生的总分排序,
所以可以使用 TreeSet集合。
3,将集合的信息写入到一个文件中。
代码示例:
import java.io.*; import java.util.*; class Student implements Comparable<Student> { private String name; private int math,cn,en; private int sum; Student(String name,int math,int cn,int en){ this.name = name; this.math = math; this.cn = cn; this.en = en; sum = math + cn + en; } //让集合的元素自身具备比较性,实现Comparable接口后,覆盖compareTo方法。 public int compareTo(Student s){ //利用Integer类的compareTo方法 int num = new Integer(this.sum).compareTo(new Integer(s.sum)); if(num==0) return this.name.compareTo(s.name); return num; } public String getName(){ return name; } public int getSum(){ return sum; } public int hashCode(){ return name.hashCode()+sum*78; } public boolean equals(Object obj){ if(!(obj instanceof Student)) throw new ClassCastException("类型不匹配"); //RuntimeException 的子类。 Student s = (Student)obj; return this.name.equals(s.name) && this.sum==s.sum; //姓名相同且总分相同,则为同一个学生。 } public String toString(){ return "Student["+name+", "+math+", "+cn+", "+en+", "+"]"; } } class StudentInfoTool{ //实现Comparable接口的自然顺序来比较,即学生的总分从小到大。 public static Set<Student> getStudents() throws IOException{ return getStudents(null); } //集合中,以指定比较器cmp的顺序来比较。 public static Set<Student> getStudents(Comparator<Student> cmp) throws IOException{ BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); //捕获键盘录入 Set<Student> stus = null; //存放学生对象的集合 if(cmp==null) stus = new TreeSet<Student>(); else stus = new TreeSet<Student>(cmp); //定义集合的比较性 String line = null; while((line=bufr.readLine())!=null){ if(line.equals("over")) break; String[] info = line.split(","); //parseInt:字符串转换成Int类型。 Student stu = new Student(info[0],Integer.parseInt(info[1]), Integer.parseInt(info[2]), Integer.parseInt(info[3])); stus.add(stu); //每创建一个学生对象,则存入集合。 } bufr.close(); return stus; } //集合中的数据打印到文件中 public static void writeToFile(Set<Student> stus) throws IOException{ BufferedWriter bufw = new BufferedWriter(new FileWriter("e:\\stu_info.txt")); for(Student stu : stus) { bufw.write(stu.toString()+"\t"); //"\t" 制表符 bufw.write(stu.getSum()+""); bufw.newLine(); bufw.flush(); } bufw.close(); } } class StudentInfoTest{ public static void main(String[] args) throws IOException{ //返回一个比较器,它强行逆转实现了Comparable接口的对象collection的自然顺序,即以学生总分的从大到小。 Comparator<Student> cmp = Collections.reverseOrder(); Set<Student> stus = StudentInfoTool.getStudents(cmp); //以比较器cmp的顺序比较。 StudentInfoTool.writeToFile(stus); } }
相关文章推荐
- IO流__【转换流的字符编码】【编码与解码】【字符编码-“联通”】
- java基础之IO转换流--OutputStreamWriter(字符流通往字节流,可以指定字符编码的IO流对象)
- 【java基础】采用字节流的文件IO演示,字节流与字符流转换,转换流的编码解码
- 黑马程序员--转换流对象,输出方向,转换流编码效果,字符的编码和解码,String类的构造,方法getBytes,指定编码表,File类
- 字符与字节的转换:编码与解码的关系
- 简单的 io流文件复制(字符流编码乱码转换)
- 练习 2017-8-14 缓冲字节/字符 输入/输出 流~ 字符的编码和解码
- C语言编码与字符转换
- 字符串到字节数组和字节数组到字符串的转换(编码和解码问题)
- html 实体编码转换成原字符
- IO流(转换流的编码解码)
- 把字符转换成un编码。。。
- unicode编码、字符的转换和得到汉字的区位码
- 用二进制来编码字符串"adceadaa",需要能够相据编码,解码回原来的字符串,则至少需要二进制字符的长度是?
- 字符编码知识:Unicode、UTF-8、ASCII、GB2312等编码之间是如何转换的?
- 黑马程序员_Java基础_IO流_编码表,编码与解码
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- Golang 奇葩的时间包之Json与struct对象之间互相转换,JsonDate和JsonTime自定义类型如何实现json编码和解码(二)
- php正确解码javascript中通过escape编码后的字符
- MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码)