posts - 20,comments - 6,trackbacks - 0

 

public class Student implements Comparable {
    
    
private String userid;
    
private String username;
    
private int mScore;
    
private int eScore;
    
private int cScore;
    
private int total;
    
    
public Student(String userid, String username, int mscore, int escore,
            
int cscore) {
        
super();
        
this.userid = userid;
        
this.username = username;
        mScore 
= mscore;
        eScore 
= escore;
        cScore 
= cscore;
    }

    
/**
     * 
@return the userid
     
*/

    
public String getUserid() {
        
return userid;
    }

    
/**
     * 
@param userid the userid to set
     
*/

    
public void setUserid(String userid) {
        
this.userid = userid;
    }

    
/**
     * 
@return the username
     
*/

    
public String getUsername() {
        
return username;
    }

    
/**
     * 
@param username the username to set
     
*/

    
public void setUsername(String username) {
        
this.username = username;
    }

    
/**
     * 
@return the mScore
     
*/

    
public int getMScore() {
        
return mScore;
    }

    
/**
     * 
@param score the mScore to set
     
*/

    
public void setMScore(int score) {
        mScore 
= score;
    }

    
/**
     * 
@return the eScore
     
*/

    
public int getEScore() {
        
return eScore;
    }

    
/**
     * 
@param score the eScore to set
     
*/

    
public void setEScore(int score) {
        eScore 
= score;
    }

    
/**
     * 
@return the cScore
     
*/

    
public int getCScore() {
        
return cScore;
    }

    
/**
     * 
@param score the cScore to set
     
*/

    
public void setCScore(int score) {
        cScore 
= score;
    }

    
public Student() {
        
super();
        
// TODO Auto-generated constructor stub
    }

    
public int compareTo(Object obj) {
        
if(this.getTotal()>((Student)obj).getTotal()){
            
return 0;
        }
else{
            
return 1;
        }

    }

    
/**
     * 
@return the total
     
*/

    
public int getTotal() {
        
return total;
    }

    
/**
     * 
@param total the total to set
     
*/

    
public void setTotal(int total) {
        
this.total = total;
    }

    
/* (non-Javadoc)
     * @see java.lang.Object#toString()
     
*/

    @Override
    
public String toString() {
        StringBuilder sb 
= new StringBuilder();
        sb.append(
this.getUserid()+" ");
        sb.append(
this.getUsername()+"");
        sb.append(
this.getMScore()+" ");
        sb.append(
this.getEScore()+" ");
        sb.append(
this.getCScore()+" ");
        sb.append(
this.getTotal());
        
return sb.toString();
    }

    
}

 

 



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ReadFileUtil {
    
public static List<Student> ReaderFileUtil(String filepathstring) {
        BufferedReader in
=null;
        List
<Student>  students = new ArrayList<Student>();
        
try {
            in 
= new BufferedReader(new FileReader(filepathstring));
            String line 
= null;
            
while ((line = in.readLine()) != null{
                 String[] information 
= new String[5];
                 information 
= line.split(",");
                 Student temp 
= new Student();
                 temp.setUserid(information[
0]);
                 temp.setUsername(information[
1]);
                 temp.setMScore(Integer.parseInt(information[
2]));
                 temp.setEScore(Integer.parseInt(information[
3]));
                 temp.setCScore(Integer.parseInt(information[
4]));
                 students.add(temp);
            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }
 finally{
            
try {
                in.close();
            }
 catch (IOException e) {
                e.printStackTrace();
            }

        }

        
return students;
    }

    
    
public static List<Student> getTotals(List<?> list){
        
int total=0;
        List
<Student> exchangeStudent=new ArrayList<Student>();
        
//注意这个地方的list是传过来的参数!!!
        Iterator<?> iter=list.iterator();
        
while(iter.hasNext()){
            Student stu
=(Student)iter.next();
            total
=stu.getCScore()+stu.getEScore()+stu.getMScore();
            stu.setTotal(total);
            exchangeStudent.add(stu);
        }

        
return exchangeStudent;
    }

    
    
public static void writeFileProperty(List<Student> list,String filepathstring){
        BufferedWriter out 
= null;
        
try {
            out 
= new BufferedWriter(new FileWriter(filepathstring));
            
//这个地方传的是一个list
            Iterator it=list.iterator();
            
while(it.hasNext()){
                Student stu
=(Student) it.next();
                out.write(stu.getUserid()
+","+stu.getUsername()+","+stu.getTotal()+";");//把你想写入到文件里的内容写到文件里去
                out.newLine();//文件中的换行
            }

        }
 catch (IOException e) {
            e.printStackTrace();
        }
finally{
            
try {
                out.close();
            }
 catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}





import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class ReaderFileProperty {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        BufferedReader in 
= null;
        Collection  students 
= new ArrayList();
        
try {
            in 
= new BufferedReader(new FileReader("input.txt"));
            String line 
= null;
            
while ((line = in.readLine()) != null{
                 String[] information 
= new String[5];
                 information 
= line.split(",");
                 Student temp 
= new Student();
                 temp.setUserid(information[
0]);
                 temp.setUsername(information[
1]);
                 temp.setMScore(Integer.parseInt(information[
2]));
                 temp.setEScore(Integer.parseInt(information[
3]));
                 temp.setCScore(Integer.parseInt(information[
4]));
                 temp.setTotal(temp.getCScore()
+temp.getEScore()+temp.getMScore());
                 students.add(temp);
            }

        }
 catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
 finally {
            
try {
                in.close();
            }
 catch (IOException e) {
                e.printStackTrace();
            }

        }

        
        Iterator it 
= students.iterator();
        
for(Student student: (Collection<Student>)students){
//            System.out.println(student);
            System.out.println(student.getUsername());
        }


    }


}



import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class TestFile {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        String filepath
="input.txt";
        String filepath2
="f:\\some.txt";
        List list
=com.yuer.txt.ReadFileUtil.ReaderFileUtil(filepath);

        List resultList
=com.yuer.txt.ReadFileUtil.getTotals(list);
        Iterator iter
=resultList.iterator();
        
while(iter.hasNext()){
//            System.out.println(iter.next());
            Student stu=(Student) iter.next();
            System.out.println(
"我的姓名:"+stu.getUsername()+";"+",我的语文成绩:"+stu.getCScore()+",我的英语成绩:"+stu.getEScore()+",我的数学成绩:"+stu.getMScore()+",我的总分是:"+stu.getTotal());
        }

        Collections.sort(resultList);
        Iterator it
=resultList.iterator();
        
while(it.hasNext()){
            Student stu
=(Student) it.next();
            System.out.println(
"我的姓名:"+stu.getUsername()+";"+",我的语文成绩:"+stu.getCScore()+",我的英语成绩:"+stu.getEScore()+",我的数学成绩:"+stu.getMScore()+",我的总分是:"+stu.getTotal());
        }

        com.yuer.txt.ReadFileUtil.writeFileProperty(resultList, 
"f:\\output.txt");
    }


}

posted on 2009-03-10 17:43 Johnhe 阅读(136) 评论(0)  编辑  收藏 所属分类: J2SE

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问