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();
}
}
javaio
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();
}
}
javaio
如果报错就把错误贴出来。
另外你TXT去重的单位是什么?
行还是单位值?
另外排序的按照的规律是啥?
timeContent.setContent(row[1]);//这里数组下标超出了
{
//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
}
}
排序首先比较time,若time相同的话在比较后面的content
楼主先去掉去重的操作,先跑通排序。
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.
}