编码:字符串变成字节数组
解码:字节数组变成字符串。
String-->byte[]; str.getBytes(charsetName);
byte[]-->String: new Strng(byte[],charsetName);
class EncodeDemo{
public static void main(String[] args) throw Exception{
String s="你好";
byte[] b1 = s.getBytes("GBK");
//System.out.println(Arrays.toString(b1));
String s1=new String(b1,"GBK");
System.out.println("s1="+s1);
}
}
练习:
有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括姓名,3门课成绩,如zhangsan,30,40,60),计算总成绩,并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。
/*
1、描述学术对象
2、定义一个可以操作学生对象的工具类
思想:
1、通过获取键盘录入的数据,将该行信息取出封装成学生对象。
2、因为学生有很多,需要存储,使用集合,对学生的总分排序,可以使用treeset
3、将集合中信息写入到一个文件中
*/
class Student implements Comparable<Student>{
private String name;
private double ch,ma,en;
private double sum;
Student(String name,double ch,double ma,double en){
this.
this.
this.
this.
sum=ch+ma+en;
}
public int compareTo(Student s){
int num=new Integer(this.sum).compareTo(new
}
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(" ");
Student s=(Student)obj;
return this.name.equals(s.name)&&this.sum==s.sum;
}
}
public String toString(){
return "student:"+name+","+ch+","+ma+","+en;
}
}
class StudentInfoTool{
public static Set<Student> getStudents(){
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)){
if
}
}
public static void main(String[] args){
}
}