Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ANT opening and writing into a file

Posted on 2004-11-19
9
Medium Priority
?
1,704 Views
Last Modified: 2008-02-01
Hi
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!").
Thanks
NGujja
0
Comment
Question by:ngujja
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12626985
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" />
    </copy>

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

MIDlet-Jar-Size: 120000
....

0
 
LVL 8

Assisted Solution

by:kiranhk
kiranhk earned 500 total points
ID: 12627035
You need to create a task of your own and then write whatever contents you want into that file.


import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class MyTask extends Task {
    private String msg;

    // The method executing the task
    public void execute() throws BuildException {
        System.out.println(msg);
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
            out.write(msg);
            out.close();
    } 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.

Example

<?xml version="1.0"?>

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

hope this helps
0
 
LVL 13

Accepted Solution

by:
petmagdy earned 500 total points
ID: 12627366
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" />

</target>

</project>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ngujja
ID: 12627442
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.
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12627461
please also see my last comment
0
 

Author Comment

by:ngujja
ID: 12627586
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.
0
 

Author Comment

by:ngujja
ID: 12629560
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.
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12646622
use &gt; and &lt; for the same
0
 

Author Comment

by:ngujja
ID: 12649767
Hey Kiran Thanks I had got the solution.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question