Beginner Outputing to a file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 2
Reputation: Hal is an unknown quantity at this point 
Solved Threads: 0
Hal Hal is offline Offline
Newbie Poster

Beginner Outputing to a file

 
0
  #1
Mar 11th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Beginner Outputing to a file

 
0
  #2
Mar 11th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 2
Reputation: Hal is an unknown quantity at this point 
Solved Threads: 0
Hal Hal is offline Offline
Newbie Poster

Re: Beginner Outputing to a file

 
0
  #3
Mar 12th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Beginner Outputing to a file

 
0
  #4
Mar 12th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 50
Reputation: AstroNox is an unknown quantity at this point 
Solved Threads: 2
AstroNox AstroNox is offline Offline
Junior Poster in Training

Simple File Output Example

 
0
  #5
Mar 19th, 2006
Writing to a file is pretty simple with Java. Here's some sample code:

  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
Best Regards, God Bless,
AstroNox
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC