从文件中读取学生信息和分数,求出平均分后升序写入另一个文件。

Student类,代码如下:

package student;

import java.text.DecimalFormat;

public class Student {
	private int id;//学号
	private String name;//姓名
	private int score1;//成绩1
	private int score2;//成绩1
	private int score3;//成绩1
	private double avg;//平均值
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getScore1() {
		return score1;
	}
	public void setScore1(int score1) {
		this.score1 = score1;
	}
	public int getScore2() {
		return score2;
	}
	public void setScore2(int score2) {
		this.score2 = score2;
	}
	public int getScore3() {
		return score3;
	}
	public void setScore3(int score3) {
		this.score3 = score3;
	}
	public double getAvg() {
		return avg;
	}
	public void setAvg(double avg) {
		this.avg = avg;
	}
	@Override
	public String toString() {
		return id + " " + name + " " + score1 + " " + score2 + " " + score3 + " " +new DecimalFormat("0.00").format(avg) + "\r\n";
	}

}
ReadData类,代码如下:

package student;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;

import student.Student;

public class ReadData {
	public void Read(Map> map,String txt){
		//读入数据
		//String fileName="data/11.txt";
		try {
			BufferedReader reader = new BufferedReader(new FileReader(txt));
			String line = null;
	        
	        while((line=reader.readLine())!=null){//循环赋值读入
	        	String str[] = line.split("\\s+");
	        	Student op=new Student();
	        	op.setId( Integer.parseInt(str[0].trim()));
	        	op.setName( str[1].trim());
	        	op.setScore1(Integer.parseInt(str[2].trim()));
	        	op.setScore2(Integer.parseInt(str[3].trim()));
	        	op.setScore3(Integer.parseInt(str[4].trim()));
	        	double k=(Integer.parseInt(str[2].trim())+Integer.parseInt(str[3].trim())+Integer.parseInt(str[4].trim()))/3.0;
	        	op.setAvg(k);
	        	if(map.containsKey(op.getAvg())){
	        		map.get(op.getAvg()).add(op);
	        	}else{
	        		TreeSet ts=new TreeSet();
	        		ts.add(op);
	        		map.put(op.getAvg(), ts);
	        	}
	        }
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void OutPrint(Map> map,String str){
		//按一定的顺序输出信息
		//System.out.println("学号  姓名  性别  地址    java语言成绩    C语言成绩    编译原理成绩    JavaEE成绩");
		try {
		BufferedWriter writer = new BufferedWriter(new FileWriter(new File(str), false));
		for(Iterator it = map.keySet().iterator();
		             it.hasNext();)
		{
			TreeSet tp2=new TreeSet();
			tp2.addAll(map.get(it.next()));
			for(Iterator it1 = tp2.iterator();
             it1.hasNext();)
			{
				writer.write(it1.next().toString()+"\n");
			}
			//writer.write("\n");
			System.out.println("程序执行中!");
		}
		writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
	}

}

Main类,代码如下:

package student;

import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Map> map=new TreeMap>();
		ReadData op=new ReadData();
		String txt1="score.txt";
		String txt2="scoreSort.txt";
		op.Read(map,txt1);
		op.OutPrint(map, txt2);
		System.out.println("程序执行完毕!");

	}

}

以上代码可能存在一些冗余的部分,没有进行修改,如果需要可以自行拷贝和修改。





你可能感兴趣的:(java)