风动还是旗动?仁者心动。 文字只不过是指月的手指,而不是月亮本身,能透过指月的手指看到月亮本身才是文字的目的所在。

ant学习-扩展编程

上一篇 / 下一篇  2007-01-23 17:17:39 / 个人分类:ant学习

ODh,s0r F*Iz.Y01、新建的类必须是Task的扩展51Testing软件测试网Q4U Of!J(Q*y [;H

51Testing软件测试网+qDM-|KxW

2、类中必须重写execute()方法

4l*ZAM+_5d H0

V0PR+J @ n0Rw"K/P.uR0下面是一个对文件排序的扩展任务类51Testing软件测试网/nI!cn$k^9o

^%ep(g s De.N8_A0import org.apache.tools.ant.BuildException;51Testing软件测试网X;y } gz
import org.apache.tools.ant.Task;51Testing软件测试网kV0p9J}Vu
import java.io.*;51Testing软件测试网t-N8F&F f"ic?9US
import java.util.*;

ABT'iJYa8i0

] NR,r`0/**
M0mjYH0J K0 *
.Wz!Ems.~ CGTN0 * @author xugang51Testing软件测试网)it(D,}?2~w
 * A simple example task to srot a file51Testing软件测试网 _ SlitQVT n
 *
+Fg2}4D|hu2Vv^0 */

r~/W7GKxf8|051Testing软件测试网5PZ C@ r"K$n

public class FileSorter extends Task {
bV T5?"GSuBz b0 private File file, toFile;51Testing软件测试网%h4P_2`#e1x2b
 51Testing软件测试网+I|0r5R&xH@d"zx-}
 //The method excuting the task
9Uv/e;|W[aWk0 public void execute() throws BuildException{51Testing软件测试网'W6o\Sx[I:AV:F
  System.out.println("Sorting file=" +file);51Testing软件测试网 Uz;a D$N
  try{
6|"Nm-y(Z0   BufferedReader from =
gu.k6]*S5h/KB7WC g0    new BufferedReader(new FileReader(file));
V(PR2|8]3d0   BufferedWriter to =51Testing软件测试网&}W!l)d5AP
    new BufferedWriter(new FileWriter(toFile));51Testing软件测试网7?*\bwm/x}:gncJ!iq
   List allLines = new ArrayList();
,Od"p K%s0   51Testing软件测试网5j}~~ k3_'vA
   //read in the input file51Testing软件测试网G6];o/gf
   String line = from.readLine();
PkRuo.F~ Y0   while (line != null)
x'YnZ mgn9eCY0   {
xh7k6?+wa0    allLines.add(line);
*A!]u?uy'g/_0    line = from.readLine();
Z8P#FcB)ykyY0   }
m SwS&UB|s0   from.close();51Testing软件测试网7h"@ d _ Yg!g(D.O {
   
I5zV-PH0G|wC;Q0   //sort the list51Testing软件测试网'k9[&g:R2niC
   Collections.sort(allLines);
q g8C$p;PuI[H d0   
B;Iv3U"g}_+w7xM| Q0   //write out the sorted list51Testing软件测试网hC'V&Kwn_B&Y LH
   for (ListIterator i = allLines.listIterator();i.hasNext();){51Testing软件测试网4gF s;`ow.Zll}:g
    String s = (String)i.next();
mrlaCSi2]0    to.write(s);51Testing软件测试网jX(O!Wjn\d
    to.newLine();51Testing软件测试网 {o,IbDC6e
   }
-l5Fq@+U`om]0   to.close();
:_0g,vIx7oBh0    51Testing软件测试网,B_'`v[)]n
  }catch (FileNotFoundException e){throw new BuildException(e);}51Testing软件测试网z6yw] F"M4]*f
  catch(IOException e){throw new BuildException(e);}51Testing软件测试网Pf;p'{s,xM]@&M
  
m:Q r} b u:z0  51Testing软件测试网 f!p3XhQyy kk!\~
 }
g3p-sy%K_$e5D0 
E2A9wD1H_0 //The setter for the "file" attribute51Testing软件测试网|V*O#V N
 public void setFile(File file){51Testing软件测试网l'KN"c ii
  this.file = file;51Testing软件测试网M%ai,x cC"?
 }
3M9an6w$m3u|!U0 51Testing软件测试网n|F*Siz
 //The setter for the "tofile" attribute
?^ D${]'H8ME0 public void setToFile(File toFile){
5j8Fdv%jP0  this.toFile = toFile;51Testing软件测试网Fe.O9g.E{5U#K
 }
#f-uV"i-P6} izr0}
bH y/[P/Qk])qp051Testing软件测试网#G#b:I1zKDi(j

,r&Y^O CI0在ant中可以这样调用:51Testing软件测试网.|8B7{yGois `

2t Z'`z-OT \ `)N~0<?xml version="1.0"?>51Testing软件测试网iL ?)?(N3I OW'x
<project name = "AntTask" default="main" basedir=".">
4G.w;F"KH5\}U&?0<taskdef name="filesorter" classname="com.stu.task.FileSorter" classpath="./bin"/>
`+X U*N\.jc"u#J0 <target name = "main">51Testing软件测试网:JRUR%H"E(u
  <filesorter file = "input.txt" toFile = "output.txt"></filesorter>51Testing软件测试网7C(Z6p]9R:Zxh
 </target>
&J5F'JmJ4u x2}5Af0</project>51Testing软件测试网#L$i#y7y%j2[LG,h
51Testing软件测试网 CS4C3|#?F

w0w@Y.FxO_!J0其中<taskdef name="filesorter" classname="com.stu.task.FileSorter" classpath="./bin"/>定义扩展的任务。51Testing软件测试网.B:q U9V7my

51Testing软件测试网m,_ a)g*h ]

这个是filesorter任务<filesorter file = "input.txt" toFile = "output.txt"></filesorter>

5k3K2}P6l ld7ew@e051Testing软件测试网8\,Za"_8W[U{

该任务的目标是将input.txt中的行进行排序,并写到output.txt文件中51Testing软件测试网%Ha*^-I*x

3T"v0T#~.r4cYN"ym0 

N.?6[6yU,l0

TAG: ant学习

 

评分:0

我来说两句

日历

« 2024-01-16  
 123456
78910111213
14151617181920
21222324252627
28293031   

数据统计

  • 访问量: 25203
  • 日志数: 36
  • 建立时间: 2006-12-19
  • 更新时间: 2007-07-09

RSS订阅

Open Toolbar