1. 程式人生 > >java 將學生列表資訊按照成績進行排序,並建立新的檔案儲存

java 將學生列表資訊按照成績進行排序,並建立新的檔案儲存

1 張三1 18 男 88 1385551 2 張三 18 女 99 1385552 3 張三 18 男 88 1385553 4 李四1 19 男 66 1385554 5 李四 20 女 88 1385555 6 李四 19 男 66 1385556 7 王五 20 女 87 1385557 8 趙六 18 男 88 1385558 9 周七 19 女 99 1385559 10 馬八 18 男 88 1385550 ------------- 讀取檔案student.txt資料: 1.按成績進行降序排列後 寫入新檔案newStudent.txt, 所在資料夾可以隨意指定。 2.把姓名相同的同學資訊 寫入日誌檔案student_log.txt 日誌檔案要求: 新增資訊時間例如 2018-9-17 9:32:52 2 張三 18 女 99 3 張三 18 男 88 5 李四 20 女 88 6 李四 19 男 66 2018-9-17 11:31:18 2 張三 18 女 99 3 張三 18 男 88 5 李四2 20 女 88 6 李四2 19 男 66

學生類;


public class Student {
	private int id;
	private String name;
	private int age;
	private String sex;
	private float grade;
	private String tel;
	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 getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public float getGrade() {
		return grade;
	}
	public void setGrade(float grade) {
		this.grade = grade;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	
}

import java.io.BufferedWriter;
import java.io.*;
import java.io.FileWriter;
import java.util.*;
 
public class Demo03 {
    public List<Student> setList(String path) {
        List<Student> list = new ArrayList<Student>();
        FileReader fr;
        try {
            fr = new FileReader(path);
            BufferedReader br = new BufferedReader(fr);
            String s = br.readLine();
            while (s != null) {
                Student stu = new Student();
                String str[] =s.split(" ");
                stu.setId(Integer.parseInt(str[0]));
                stu.setName(str[1]);
                stu.setAge(Integer.parseInt(str[2]));
                stu.setSex(str[3]);
                stu.setGrade(Float.parseFloat(str[4]));
                stu.setTel(str[5]);
                list.add(stu);
                s = br.readLine();
            }
            br.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    public List<Student> array(List<Student> list){
        for (int i = 0; i <list.size()-1 ; i++) {
            for (int j = 0; j <list.size()-1 ; j++) {
                if(list.get(j).getGrade()<list.get(j+1).getGrade()){
                    Student s=list.get(j);
                    list.set(j,list.get(j+1));
                    list.set(j+1,s);
                }
            }
        }
        return list;
    }
   public void write(String newpath,List<Student> list) throws IOException {
       FileWriter fw=new FileWriter(newpath);
       BufferedWriter bw=new BufferedWriter(fw);
       for (int i = 0; i <list.size() ; i++) {
           bw.write(list.get(i).getId() + "\t" +list.get(i).getName() + "\t" +
                   list.get(i).getAge() + "\t" + list.get(i).getSex() + "\t" + list.get(i).getGrade() + "\t" +
                   list.get(i).getTel()+"");
           bw.newLine();
       }
       bw.flush();
       fw.close();
   }
   public void samewrite(String otherpath,List<Student> list) throws IOException {
       FileWriter fw=new FileWriter(otherpath);
       BufferedWriter bw=new BufferedWriter(fw);
       for (int i = 0; i <list.size() ; i++) {
           for (int j = 0; j <list.size() ; j++) {
               if(list.get(1).getName()==list.get(2).getName()&&i!=j){
                   /*bw.write(list.get(i).getId() + "\t" +list.get(i).getName() + "\t" +
                           list.get(i).getAge() + "\t" + list.get(i).getSex() + "\t" + list.get(i).getGrade() + "\t" +
                           list.get(i).getTel()+"");*/
                   bw.newLine();
                   System.out.println(list.get(i).getId() + "\t" +list.get(i).getName() + "\t" +
                           list.get(i).getAge() + "\t" + list.get(i).getSex() + "\t" + list.get(i).getGrade() + "\t" +
                           list.get(i).getTel()+"");
                   bw.write(list.get(j).getId() + "\t" +list.get(j).getName() + "\t" +
                           list.get(j).getAge() + "\t" + list.get(j).getSex() + "\t" + list.get(j).getGrade() + "\t" +
                           list.get(j).getTel()+"");
                   bw.newLine();
                   System.out.println(list.get(j).getId() + "\t" +list.get(j).getName() + "\t" +
                           list.get(j).getAge() + "\t" + list.get(j).getSex() + "\t" + list.get(j).getGrade() + "\t" +
                           list.get(j).getTel()+"");
               }
           }
       }
       bw.flush();
       fw.close();
 
   }
    public static void main(String[] args) throws IOException {
        Demo03 d = new Demo03();
        String path = "e:/student.txt";
        List<Student> list = d.setList(path);
       /* System.out.println("排序前");
        for (Student stu : list) {
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" +
                    stu.getAge() + "\t" + stu.getSex() + "\t" + stu.getGrade() + "\t" +
                    stu.getTel());
        }*/
//        System.out.println("排序後");
        List<Student> arraylist=d.array(list);
        for (Student stu : arraylist) {
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" +
                    stu.getAge() + "\t" + stu.getSex() + "\t" + stu.getGrade() + "\t" +
                    stu.getTel());
        }
        String newpath="D:/newstudent.txt";
        d.write(newpath,arraylist);
        String otherpath="D:/student_log.txt";
        d.samewrite(otherpath,list);
    }
}