I have doubts with the 25-part ...

Quick hack:

<project>
    <taskdef name="insert" classname="InsertTask" classpath="."/>
    <insert inputfile="orig.txt" outputfile="new.txt" after="5">
        The text to be inserted.
        Maybe multiple lines?
    </insert>
</project>



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.io.LineNumberReader;

public class InsertTask {
    
    int after;
    File inputfile;
    File outputfile;
    String text = "";
    

    public void execute() {
        try {
            LineNumberReader inp = new LineNumberReader (new
FileReader(inputfile));
            BufferedWriter   out = new BufferedWriter(new
FileWriter(outputfile));
            String line;
            while ((line = inp.readLine()) != null) {
                out.write(line);
                out.write(System.getProperty("line.separator"));
                if (inp.getLineNumber()==after) {
                    out.write(text);
                    out.write(System.getProperty("line.separator"));
                }
            }
            inp.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void addText(String text) {
        this.text += text;
    }
    public void setAfter(int after) {
        this.after = after;
    }
    public void setInputfile(File inputfile) {
        this.inputfile = inputfile;
    }
    public void setOutputfile(File outputfile) {
        this.outputfile = outputfile;
    }
    
    public static void main(String[] args) {
        InsertTask ins = new InsertTask();
        ins.setInputfile(new File(args[0]));
        ins.setOutputfile(new File(args[1]));
        ins.setAfter(Integer.parseInt(args[2]));
        for(int i=3; i<args.length; i++) ins.addText(args[i]+" ");
        ins.execute();
    }
}


Jan

> -----Ursprüngliche Nachricht-----
> Von: Rebhan, Gilbert [mailto:[EMAIL PROTECTED]
> Gesendet am: Freitag, 28. Januar 2005 12:20
> An: user@ant.apache.org
> Betreff: regexp question
> 
> 
> Hi,
> 
> scenario = a txtfile where i have to put a value in.
> 
> The line where that value should go is always line number 26 of
> that file. line number 26 is always blank and should contain the
> value after transformation.
> 
> I've tried with :
> 
> <property name="br" value="${line.separator}" />
> <property name="insert" value="hello world" />
> 
> 
> <copy file="${txtfile}" tofile="modified.txt">
>     <filterchain>
>         <tokenfilter>
>             <filetokenizer />
>                 <replaceregex
> pattern="(.*${br}{25})(${br}.*)"
>                       replace="\1${insert}\2" />
>         </tokenfilter>
>     </filterchain>
> </copy>
> 
> The script runs, but the file stays the same.
> Must be a logical error in the regex syntax.
> 
> (.*${br}{25})(\s${br})
> 
> group 1 = any sign, one or more, followed by linebreak, 25x 
> (= 25 lines)
> group 2 = the next (blank) line
> 
> replace \2 = replace group 2 with ${insert}
> 
> Where do i go wrong ? Don't know exactly how to write the 
> replace part.
> My regexengine is the java builtin (1.4.2_05)
> 
> Thanks for any hint !
> 
> Gilbert
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

Reply via email to