67,512
社区成员
发帖
与我相关
我的任务
分享
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class FileSorter extends Task {
private File file;
private File tofile;
public void setFile(File file) {
this.file = file;
}
public void setTofile(File tofile) {
this.tofile = tofile;
}
@SuppressWarnings("unchecked")
@Override
public void execute() throws BuildException {
System.out.println("Sorting file="+file);
try {
BufferedReader form=new BufferedReader(new FileReader(file));
BufferedWriter to=new BufferedWriter(new FileWriter(tofile));
List allList=new ArrayList();
String line=form.readLine();
while(line!=null){
allList.add(line);
line=form.readLine();
}
form.close();
//sort the lise
Collections.sort(allList);
//write out the sorted list
for(ListIterator i=allList.listIterator();i.hasNext();){
String s=(String)i.next();
to.write(s);
to.newLine();
}
to.close();
} catch (FileNotFoundException e) {
throw new BuildException();
//e.printStackTrace();
} catch(IOException e){
throw new BuildException();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project name="CustomTaskExample" default="main" basedir=".">
<property name="src" location="src"/>
<property name="compile" location="classes"/>
<taskdef name="filesorter" classname="FileSorter" classpath="classes" />
<target name="main" depends="compile">
<filesorter file="input.txt" tofile="output.txt"/>
</target>
<target name="compile">
<mkdir dir="${compile}"/>
<javac srcdir="${src}" destdir="${compile}"/>
</target>
</project>
执行build.xml文件时发生错误:
F:\workstudy\ant\build1.xml:22: taskdef class FileSorter cannot be found
请教各位,该如何解决,谢谢。