Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2005

Tip: Looking for answers? Try searching our database.

printing to a file

Thread view: 
omaeva@gmail.com - 28 Sep 2005 00:26 GMT
i am printin an output to a file
but i need all the data, but for some reason my dad gets written over
by the following at each iteration. here is my algorithm.
bascially i need a list of t(n) in a file:

public static void main (String str[]){

        long n, t;

        //timing from 0 to 5000

        for (n=1; n<5001; n++){

        long startTime = System.currentTimeMillis(); // get starting time

        LargeFact.fact(n);//call fact to run LargeFact

        long endTime = System.currentTimeMillis(); // get end time

        t = endTime - startTime; //end time minus start is total time

        System.out.println (t +"(" +n+ ")");

        // write to file tofn.txt
        // with the help of javaalmanac.com
        try{

        BufferedWriter out = new BufferedWriter(new FileWriter("tofn.txt"));
        out.write(t + "("+n+")");
        out.flush();
       out.close();
       }

       catch (IOException e){}   
       
       
}       

}
klynn47@comcast.net - 28 Sep 2005 01:25 GMT
When you use the FileWriter constructor that accepts just the name of a
file, then that file will be overwritten. If you create a FileWriter
object with a second parameter true, then the file will be appended.
Alan Krueger - 28 Sep 2005 03:00 GMT
> i am printin an output to a file
> but i need all the data, but for some reason my dad gets written over
> by the following at each iteration.

Try creating the output writer once rather than 5001 times.  Each time
you create the FileWriter, you overwrite the previous contents of the file.
Roedy Green - 28 Sep 2005 03:39 GMT
>BufferedWriter out = new BufferedWriter(new FileWriter("tofn.txt"));
>        out.write(t + "("+n+")");
>        out.flush();
>        out.close();

You create a new file tofn.txt on every iteration.  There are two
approaches.  The more likely one it is open the file at the before the
loop and close it after the loop.  

The other, which is thousands of times more costly in computer time,
is to use the append option. For example of use consult
http://mindprod.com/applets/fileio.html
It has the advantage it will keep accumulating even across sessions.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.