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.