I am new to Java, in fact I am just learning. I am have captured the location of an ant object as it moves across the frame and printed it to the screen. I now want to capture those locations at various times and write them to a file as Xcoord (float) and Ycoord(float).
I have read several sources on the web about java.io, and am very confused. Will someone give this newbee a hand?

Thanks,

Beginner Hal

I assume you need help in getting those coordinates???

If so, then it should be fairly simple. If the object is moving across the screen, then obviously you are doing something to change it's coordinates. Just get the values that are changing. If you provide more information I can help you more.

I assume you need help in getting those coordinates???
If so, then it should be fairly simple. If the object is moving across the screen, then obviously you are doing something to change it's coordinates. Just get the values that are changing. If you provide more information I can help you more.

Thanks for the reply.

Capturiing the coordinates was not the problem. My program captures them and immediately prints them to the screen without putting them in an array or anything. I have no idea how many coordinates there will be and cannot size an array. Therefore, what I am looking to do is write them to an outfile instead of to the screen.

(in a Constructor)

// FileWriter outfile = new FileWriter ("antlog1.txt");    // Creats a new 
                                                                // file to track 
                                                                // the ants locations

(in a Method)

 if (isAlive())
        {  
            cycle = cycle + 1;

            Footprint fp = getBounds(); 
            xCoord = (float)fp.getXOrigin();
            yCoord = (float)fp.getYOrigin();

 // Prints the current location of the ant to the screen.
               System.out.println("Ant Location is : Cycle  " + cycle + " ,  " + xCoord + ",    " + yCoord);

             // Writes the current location of the ant to the the log file.
                out.write("Ant Location is : Cycle  " + cycle + " ,  " + xCoord + ",    " + yCoord);

(I want to take the coord file and input it to ArcGIS, a Geographic Information System software to do further analysis.)

I am a beginner to java and don't understand how to write output to a file. What I have read about java.io has left me confused. FileWriter, BufferWriter, nio and channels ???!!!.

Any help or just a point to a straightforword simple source/website will be appreciated. I think I am just chasing my tail now and need to break out of the loop.

Edited 3 Years Ago by pyTony: fixed formating

I wouldn't worry about outputing to a file. That could big a consuming operation as you may be continiously writting to/from a file. Use an ArrayList. You don't have to have a definite size.

Writing to a file is pretty simple with Java. Here's some sample code:

import java.io.*;

public class DoSomeFileOutput {[INDENT]public static void main (String[] args) {[INDENT]try {[INDENT]FileWriter fw = new FileWriter("myfile.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println("Hello World!");
pw.close();[/INDENT]}
catch (IOException ioe) {[INDENT]ioe.printStackTrace();[/INDENT]}[/INDENT]}[/INDENT]}

What basically happens is that I wrapped a PrintWriter around the FileWriter . The PrintWriter provides the necessary services for String 's to be directly printed into a file. Don't forget to close the outmost stream wrapper after use!

This article has been dead for over six months. Start a new discussion instead.