| | |
Beginner Outputing to a file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 2
Reputation:
Solved Threads: 0
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 have read several sources on the web about java.io, and am very confused. Will someone give this newbee a hand?
Thanks,
Beginner Hal
•
•
Join Date: Mar 2006
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
Writing to a file is pretty simple with Java. Here's some sample code:
What basically happens is that I wrapped a
Java Syntax (Toggle Plain Text)
import java.io.*; public class DoSomeFileOutput {<blockquote>public static void main (String[] args) {<blockquote>try {<blockquote>FileWriter fw = new FileWriter("myfile.txt"); PrintWriter pw = new PrintWriter(fw); pw.println("Hello World!"); pw.close();</blockquote>} 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
AstroNox
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Help with input (C)
- Text File Input and manipulation (C++)
- Output in the text file (C)
- Need help with including C++ code in Word macro (C++)
Other Threads in the Java Forum
- Previous Thread: Recursion
- Next Thread: Need a little help
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads time tree unlimited utility webservices windows






