0

I have been trying to get console output to a text file but what I get is output of only one recursion. I am calling the function with different parameters but the output will be printed in the file just once, not for all recursions. Please help.

     File file = new File("out.txt");
     FileOutputStream fos = new FileOutputStream(file);
     PrintStream ps = new PrintStream(fos);
     System.setOut(ps);
     System.out.println("Mean is : "+meanX);
     System.out.println("Variance is :"+ variance);
     System.out.println("Standard Deviation is :"+std_deviation);
     System.out.println("CoVariance is :"+covariance);
     System.out.println("CoRelation is :"+corel);
Kyrie
  • 53
  • 7

2 Answers2

3

Since you're not showing the complete program I can only guess that that code is inside of the function you mentioned that you are calling with different parameters. If that is the case then create the FileOutputStream with append mode: FileOutputStream("out.txt", true);

vk239
  • 1,014
  • 1
  • 12
  • 30
JJF
  • 2,681
  • 2
  • 18
  • 31
1

Try to initialize your OutputStream with the constructor that takes an append flag:

FileOutputStream fos = new FileOutputStream(file, true);

This way whenever you open the file again, the new data will be added without removing the current data in the file.

I have just tested it and it works fine.

Krasimir Stoev
  • 1,734
  • 11
  • 9