ANT opening and writing into a file

I have been trying to use ANT to open a file and write content into it. I used to exec to run notepad.exe or cmd.exe and output attribute to define a file. But after creating the file I want some custom content to be written into the file. How do I do that. I thought I could use inputstring attribute but I cannot use it, I have tried redirector element.

I just want my ANT script file to open a file and write something into it (say "hello!").
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yes filters take this example very known in J2ME builds, i have a file (anything.jad) that contains something like this:

MIDlet-Jar-Size: @jarsize@

this file is considered a template i want to replace @jarsize@ with a value at running ant with the Jar size (for example)
hence I have an ant task like this:

    <filter token="jarsize" value="120000" />
    <copy todir="c:\output" filtering="true" overwrite="true">
      <fileset dir="c:\input" includes="anything.jad" />

assuming the template is at c:\input
the output at c:\output will look like this:

MIDlet-Jar-Size: 120000

You need to create a task of your own and then write whatever contents you want into that file.


public class MyTask extends Task {
    private String msg;

    // The method executing the task
    public void execute() throws BuildException {
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
    } catch (IOException e) {


    // The setter for the "message" attribute
    public void setMessage(String msg) {
        this.msg = msg;

Now you can add this in your build.xml as an ant task

   1. Make sure the class that implements your task is in the classpath when starting Ant.
   2. Add a <taskdef> element to your project. This actually adds your task to the system.
   3. Use your task in the rest of the buildfile.


<?xml version="1.0"?>

<project name="OwnTaskExample" default="main" basedir=".">
  <taskdef name="mytask" classname="MyTask"/>
  <target name="main">
    <mytask message="Hello World! MyTask works!"/>

hope this helps
if it is just writing text to file it just can be

<project name="Write file" default="all" basedir=".">

      <property name="message" value="hello dear" />

<target name="all" >
<echo message="${message}"  file="out.txt" />



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ngujjaAuthor Commented:
hey petmagdy thanks for the comment. I dont want to use filter, Iam already using it with tokens and doing pretty much what you have posted for some other task.

hey Kiran thanks for the info. seems your comment could be the solution. iam in a meeting right now, will try your suggestion soon, and let you know and give you points.
please also see my last comment
ngujjaAuthor Commented:
God I can't believe it was so simple. Thanks petmagdy. I will probably use Kiran's approach. It's more generic.
Thanks both of you.
ngujjaAuthor Commented:
Sorry to bother again folks.
How can I echo "<" or ">" character. Ant says I can't use these characters. I tried to escape them using / and \, but no success.
use &gt; and &lt; for the same
ngujjaAuthor Commented:
Hey Kiran Thanks I had got the solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.