您的位置:首页 > 其它

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐