键盘录入多名学生的信息: 格式:姓名,数学成绩,语文成绩,英文成绩,按总分由高到低 将学生的信息进行排列到文件里

主函数类:

package cn.io;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;

public class iosort {

	/**
	 * @param args
	 * @throws IOException 
	 */
	/*
	 * 例题: 键盘录入多名学生的信息: 格式:姓名,数学成绩。语文成绩,英文成绩。
	 *   按总分由高到低 将学生的信息进行排列到文件里。

*思路: * 1,使用键盘录入技术。 * 2,操作的学生信息。信息非常多,须要将信息封装成学生对象。 * 3。总分由高到低,须要排序,须要对学生对象中的总分排序。须要将多个学生对象进行容器存储。

* 4,将容器中的学生对象的信息写入到文件里。

* 容器:TreeSet * * 使用操作学生信息的工具类 */ public static void main(String[] args) throws IOException { Comparator<Student> comp = Collections.reverseOrder(); Set<Student> set = GetInfoTool.getStudents(comp); File destfile = new File("E:\3.txt"); GetInfoTool.writrFile(set,destfile); /* 输入 xiao,13,12,13 ss,13,24,24 de,254,4,2 over */ } }

操作学生信息的工具类:
<pre class="java" name="code">package cn.io;

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

public class GetInfoTool {
   
	 public static Set<Student> getStudents() throws IOException
	 {
		 return getStudents(null);
	 }
	public static Set<Student> getStudents(Comparator<Student> comp) throws IOException {
	 
		BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
	
		Set<Student> set = null;
		 if(comp!=null)
		 {
				set=new TreeSet<Student>(comp);
		 }
		 else
		 { 
			 set=new TreeSet<Student>();
		 }
		String line = null;
		while((line=buf.readLine())!=null)
		{ 
			if(line.contains("over")){
				break;
			}
		  String[] strs =line.split(",");
		  Student stu = new Student(strs[0], Integer.parseInt(strs[1]), Integer.parseInt(strs[2]), Integer.parseInt(strs[3]));
		  set.add(stu);
		}

		return set;
	}

	public static void writrFile(Set<Student> set, File destfile) {
		
		BufferedWriter bfw = null;
		
		try {
			bfw = new BufferedWriter(new FileWriter(destfile));
			for(Student stu:set)
			{ 
				bfw.write(stu.getName()+"	"+stu.getSum());
				bfw.newLine();
				bfw.flush();
			}
			
		 } catch (IOException e) {
			
			e.printStackTrace();
		 }
		finally 
		{ 
			if(bfw!=null){
				 try {
					bfw.close();
				} catch (IOException e) {
				
					e.printStackTrace();
				}
			}
		}
		
		
		
	}
 
	
	
	
}


学生信息类:

package cn.io;

public class Student implements Comparable<Student> {

	 public int getSum() {
		return sum;
	}
	public void setSum(int sum) {
		this.sum = sum;
	}
	private  String name;
	 private int grade1;
	 private int grade2;
	 private int grade3;
	 private int sum;
	public Student(String name, int grade1, int grade2, int grade3) {
		super();
		this.setName(name);
		this.setGrade1(grade1);
		this.setGrade2(grade2);
		this.setGrade3(grade3);
		this.sum=grade1+grade2+grade3 ;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getGrade1() {
		return grade1;
	}
	public void setGrade1(int grade1) {
		this.grade1 = grade1;
	}
	public int getGrade3() {
		return grade3;
	}
	public void setGrade3(int grade3) {
		this.grade3 = grade3;
	}
	public int getGrade2() {
		return grade2;
	}
	public void setGrade2(int grade2) {
		this.grade2 = grade2;
	}

	@Override
	public int compareTo(Student o) {
		// TODO Auto-generated method stub
		int temp = this.sum-o.sum;
		 return temp == 0?

this.name.compareTo(o.name):temp; } }


 

原文地址:https://www.cnblogs.com/gccbuaa/p/6767943.html