943,673 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1866
  • Java RSS
Mar 11th, 2006
0

Beginner Outputing to a file

Expand Post »
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
Similar Threads
Hal
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hal is offline Offline
2 posts
since Mar 2006
Mar 11th, 2006
0

Re: Beginner Outputing to a file

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 12th, 2006
0

Re: Beginner Outputing to a file

Quote originally posted by server_crash ...
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.
Hal
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hal is offline Offline
2 posts
since Mar 2006
Mar 12th, 2006
0

Re: Beginner Outputing to a file

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 19th, 2006
0

Simple File Output Example

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

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. public class DoSomeFileOutput {<blockquote>public static void main (String[] args) {<blockquote>try {<blockquote>FileWriter fw = new FileWriter("myfile.txt");
  4. PrintWriter pw = new PrintWriter(fw);
  5. pw.println("Hello World!");
  6. pw.close();</blockquote>}
  7. catch (IOException ioe) {<blockquote>ioe.printStackTrace();</blockquote>}</blockquote>}</blockquote>}

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!
Last edited by AstroNox; Mar 19th, 2006 at 12:36 pm. Reason: Added some explanation
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Recursion
Next Thread in Java Forum Timeline: Need a little help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC