`
Luob.
  • 浏览: 1572295 次
  • 来自: 上海
社区版块
存档分类
最新评论

java之 21天 (三)"联通" 乱码 和 IO练习

    博客分类:
  • Java
阅读更多
需要明白 UTF-8 编码 格式规则

/**
 * 记事本中的 联通的 编码问题
 *
 */
public class LiantongDemo {

	
	public static void main(String[] args) throws IOException {

		String s="联通";
		byte[] by=s.getBytes("gbk");
		System.out.println(Arrays.toString(by)); //[-63, -86, -51, -88]
		for (byte b : by) {
			System.out.println(Integer.toBinaryString(b&255)); //&255 是为了获取有效位
		}
		
		/*打印出来的 是 符合 UTF-8 编码格式.所以 记事本就开始使用  UTF-8 来编码了
		11000001   
		10101010
		11001101
		10101000*/
		System.out.println(new String(by,"UTF-8"));
	}

}



IO练习

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * 有5个学生,每个学生有3门课程的成绩
 * 从键盘输入以上数据 包括 (姓名,3门 课程的成绩).
 * 输入的格式,如: 张三,30,40,60 计算出总成绩.
 * 并把学生的信息和计算出来的总分的高低顺序存放在磁盘文件stu.txt中
 * 
 * 思路:
 * 1.描述学生对象
 * 2.定义一个可以操作学生的工具类 
 * 
 * 思想:
 * 1.通过键盘录入一行数据,并将改行中的信息取出封装成学生对象.
 * 2.因为学生有很多,那么需要存储,使用到集合,因为要对学生的总分排序 
 *   所以可以使用TreeSet.
 * 3.将集合中的信息写入到一个文件中.
 */

class Student implements Comparable<Student>{
	private String name;
	private int ma,cn,en;
	private int sum;
	
	Student(String name,int ma,int cn,int en){
		this.name=name;
		this.ma=ma;
		this.cn=cn;
		this.en=en;
		sum=ma+cn+en;
	}
	
	public String getName(){
		return name;
	}
	public int getSum(){
		return sum;
	}
	
	public int hashCode(){
		return name.hashCode()+sum*2;
	}
	
	public boolean equals(Object obj){
		if(!(obj instanceof Student)){
			throw new RuntimeException("类型不匹配");
		}
		Student s=(Student)obj;
		return name.equals(s.name) && this.sum==s.sum;
	}
	
	
	public int compareTo(Student s){
		int num=new Integer(sum).compareTo(new Integer(s.sum));
		if(num==0)
			return this.name.compareTo(s.name);
		return num;
	}
	
	public String toString(){
		return "Student["+name+", "+ma+", "+cn+", "+en+"]";
	}
}

class StudentInfoTool{
	
	public static Set<Student> getStudents() throws Exception{
		return getStudents(null);
	}
	
	public static Set<Student> getStudents(Comparator<Student> cmp) throws Exception{
		BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
		
		String line=null;
		
		Set<Student> stus=null;
		if(cmp==null)
			stus=new TreeSet<Student>();
		else{
			stus=new TreeSet<Student>(cmp);
		}
		while((line=bufr.readLine())!=null){
			if("over".equals(line))
				break;
			
			String[] info=line.split(",");
			Student stu=new Student(info[0],
					new Integer(info[1]),
					new Integer(info[2]),
					new Integer(info[3])
					);
			stus.add(stu);
		}
		bufr.close();	
		return stus;
	}

	
	public static void write2File(Set<Student> stus) throws IOException{
		BufferedWriter bufw=new BufferedWriter(new FileWriter("stuinfo.txt"));
		
		for (Student stu : stus) {
			bufw.write(stu.toString()+"\t");
			bufw.write(stu.getSum()+"");
			bufw.newLine();
			bufw.flush();
		}
		bufw.close();
	}
	
	
}

public class StudyDemo {

	public static void main(String[] args) throws Exception {
		
		Comparator<Student> cmp=Collections.reverseOrder();  //使用比较器反转
		
		Set<Student>stus=StudentInfoTool.getStudents(cmp);
		
		StudentInfoTool.write2File(stus);

	}

}


0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics