IO小练习

来源:互联网 发布:mm下载软件 编辑:程序博客网 时间:2024/04/16 16:10
import java.io.File;
import java.io.IOException;

import java.util.Set;


import inputstream.domain.Student;
import inputstream.intool.GetInfoTool;
/*
 * 练习:键盘录入学生信息,格式:姓名,成绩1,成绩2,成绩3
 * 将学生按照成绩总和进行排序,并且写入到文件中
 */
public class Test2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Set<Student> set=GetInfoTool.getStudent();
File destFile=new File("myout\\1.txt");
GetInfoTool.write2File(set, destFile);
}

}


package inputstream.domain;

public class Student implements Comparable<Student> {
private String name;
private int en;
private int ma;
private int cn;
private int sum;
public Student(String name, int en, int ma, int cn) {
super();
this.name = name;
this.en = en;
this.ma = ma;
this.cn = cn;
this.sum=en+ma+cn;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sum;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sum != other.sum)
return false;
return true;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getEn() {
return en;
}

public void setEn(int en) {
this.en = en;
}

public int getMa() {
return ma;
}

public void setMa(int ma) {
this.ma = ma;
}

public int getCn() {
return cn;
}

public void setCn(int cn) {
this.cn = cn;
}
public int getSum(){
return sum;
}
public void setSum(int sum){
this.sum=sum;
}

@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;
}

}


package  inputstream.intool;
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.Set;
import java.util.TreeSet;

import inputstream.domain.Student;

public class GetInfoTool {
public static Set<Student> getStudent() throws IOException{
//键盘录入
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
//创建一个容器存储学生对象
Set<Student> set=new TreeSet<Student>();
//获取键盘录入的信息
String line=null;
while((line=bufr.readLine())!=null){
if("over".equals(line)){
break;
}
//因为录入的数据是有规律的,可以通过指定的规则进行分割
String[] str=line.split(",");
//将数组中的元素封装成对象
Student stu =new Student(str[0],Integer.parseInt(str[1]),Integer.parseInt(str[2]),Integer.parseInt(str[3]));
set.add(stu);
}
return set;
}
/**
* 将集合中的学生信息写入到文件中
* @throws IOException 
*/
public static void write2File(Set<Student> set,File destFile) throws IOException{

BufferedWriter bufw=null;
try{
bufw=new BufferedWriter(new FileWriter(destFile));
//遍历集合
for(Student stu:set){
bufw.write(stu.getName()+"\t"+stu.getSum());
bufw.newLine();
bufw.flush();
}
}finally{
if(bufw!=null)
try {
bufw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException();
}
}

}
}

0 0
原创粉丝点击