首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java IO有关问题:将一个txt文件去重并排序输出 帮忙看上上面代码有什么有关问题

2013-01-27 
java IO问题:将一个txt文件去重并排序输出 帮忙看下下面代码有什么问题 import java.util.*import java.i

java IO问题:将一个txt文件去重并排序输出 帮忙看下下面代码有什么问题
 import java.util.*;  
import java.io.*;  
public class ResortByDel {  

public static class TimeComparator implements Comparator<TimeAndContent> {
public int compare(TimeAndContent o1, TimeAndContent o2) {
TimeAndContent timeContent1=new TimeAndContent();
TimeAndContent timeContent2=new TimeAndContent();
String time1=timeContent1.getTime();
String time2=timeContent2.getTime();
if(time1.compareTo(time2)==1){
return 1;
}else if(time1.compareTo(time2)==-1){
return -1;
}else{
return timeContent1.getContent().compareTo(timeContent2.getContent());
}


}
}     
public static void main(String[] args) throws Exception{
             FileReader fr=new FileReader("D:\\test1.txt");   
     BufferedReader br=new BufferedReader(fr);  
     List<TimeAndContent> contentList=new ArrayList<TimeAndContent>();  
     String line;   
     while((line=br.readLine())!=null) {    
     TimeAndContent timeContent=new TimeAndContent();    
     String[] row=line.split(" ",2); 
     timeContent.setTime(row[0]);
     timeContent.setContent(row[1]); 
     contentList.add(timeContent);          
     }  
 
 //去重
 Iterator<TimeAndContent> it=contentList.iterator(); 
 while(it.hasNext()){
 TimeAndContent a=it.next();
 if(contentList.contains(a)){
 it.remove();
 }else{
 contentList.add(a);
 }
 }
         //排序   
 Collections.sort(contentList , new TimeComparator());  
         StringBuilder builder = new StringBuilder();   
           for (int i = 0; i < contentList.size(); i++){  
           TimeAndContent timeContent=contentList.get(i);
               builder.append(timeContent.getTime());   
               builder.append(" ");   
               builder.append(timeContent.getContent());   
               builder.append("\r");   
             }     
            System.out.println(builder.toString());   
             write("D:\\test.txt", builder.toString());  


    }
     static class TimeAndContent {
    private String time ;
    private String content;
    public String getTime(){
    return time;
    }
    public void setTime(String time){
    this.time=time;
    }
    public String getContent(){
    return content;
    }
    public void setContent(String content){
    this.content=content;
    }  
     }
   
      public static void write(String filePath,String content) throws IOException{   
          FileWriter fw=new FileWriter(filePath);  
          BufferedWriter bw=new BufferedWriter(fw); 
          bw.write(content);   
       bw.flush();   
      bw.close();    
          }
       

java io
[解决办法]
比较器里貌似有问题,不用传进去的参数,而新建对象.


       public int compare(TimeAndContent o1, TimeAndContent o2) 
        {
            //TimeAndContent timeContent1=new TimeAndContent();
            //TimeAndContent timeContent2=new TimeAndContent();
            String time1=o1.getTime();//要用方法定义的形式参数 o1,o2,否则没意义。
            String time2=o2.getTime();
            if(time1.compareTo(time2)==1)
            {
                return 1;
            }
            else if(time1.compareTo(time2)==-1)
            {
                return -1;
            }
            else
            {
                return (o1.getContent()).compareTo(o2.getContent());//用o1,o2
            }
        }

------解决方案--------------------


去重那有问题,按楼主的代码,contentList为空了。
楼主先去掉去重的操作,先跑通排序。


[解决办法]
修改一下代码,楼主参考一下:



import java.util.*;  
import java.io.*;  
public class ResortByDel 
{  
    public static class TimeComparator implements Comparator<TimeAndContent> 
    {
        public int compare(TimeAndContent o1, TimeAndContent o2) 
        {
            String time1=o1.getTime();                                  //使用o1,o2,不能新建对象。
            String time2=o2.getTime();
            if(time1.compareTo(time2)==1)
            {
                return 1;
            }
            else if(time1.compareTo(time2)==-1)
            {
                return -1;
            }
            else
            {
                return o1.getContent().compareTo(o2.getContent());
            }
        }
    }//end class TimeComparator     
    public static void main(String[] args) throws Exception
    {
        FileReader fr=new FileReader("d:\\test1.txt");   
        BufferedReader br=new BufferedReader(fr);  
        List<TimeAndContent> contentList=new ArrayList<TimeAndContent>();
        String line;   
        while((line=br.readLine())!=null) 
        {    
            TimeAndContent timeContent=new TimeAndContent();    
            String[] row=line.split(" ",2); 
            timeContent.setTime(row[0]);
            timeContent.setContent(row[1]);

            if( !contentList.contains(timeContent))                     //在这里做去重处理。


            {
                contentList.add(timeContent);
            }
        }  
        //排序   
        Collections.sort(contentList , new TimeComparator());  
        StringBuilder builder = new StringBuilder();   
        for (int i = 0; i <contentList.size(); i++)
        {  
            TimeAndContent timeContent=contentList.get(i);
            builder.append(timeContent.getTime());   
            builder.append(" ");   
            builder.append(timeContent.getContent());   
            builder.append("\r\n");                                     //回车加换行。
        }     
        System.out.println(builder.toString());   
        write("D:\\test.txt", builder.toString());  
    }//end main()

    static class TimeAndContent 
    {
        private String time ;
        private String content;
        public String getTime()
        {
            return time;
        }
        public void setTime(String time)
        {
            this.time=time;
        }
        public String getContent()
        {
            return content;
        }
        public void setContent(String content)
        {
            this.content=content;
        }
        //重写equals()方法
        public boolean equals(Object other)
        {
            if(!(other instanceof TimeAndContent))
            {
                return false;


            }
            TimeAndContent o=(TimeAndContent)other;
            return this.time.equals(o.time)&&this.content.equals(o.content);
        }
        //重写hashCode()方法,本代码可以不重写。如要用HashSet,HashMap等类时必须重写。
        public int hashCode()
        {
            return time.hashCode()+content.hashCode();
        }
    }//end class TimeAndCountent 
   
    public static void write(String filePath,String content) throws IOException
    {   
        FileWriter fw=new FileWriter(filePath);  
        BufferedWriter bw=new BufferedWriter(fw); 
        bw.write(content);   
        bw.flush();   
        bw.close();    
    }//end write.
}

热点排行