0

So I am basically creating a bag and I was told that one of my bags must contain at least 100000 elements. This won't fit in the console window of eclipse. So writing the information to a file seems like a better idea. I have put my main method and runBigBad method here. As you can see, after runBigBag, there is more I want to do. It compiles and runs fine. However, it prints everything after the runBigBag() call into the text file. I just want the contents of runBigBag() in the text file, and the rest to print to the console window. Can someone help me out? When I try to use a finally clause and do ps.close(), it wants me to initialize ps which then makes the file null. So I'm not sure what to do unless there is a easier way.

public class TestBag {

public static <E> void main(String[] args) {
    //Start time for WHOLE PROGRAM
    final long start = System.currentTimeMillis();
    //Run small bag
    runSmallBag();
    //RESET COUNTERS TO ALLOW BIG BAG TO KEEP TRACK OF COMPLEXITY
    Bag.resetCounters();
    //Run big bag
    runBigBag();
    //Display the operation counters for each method used in whole program
    System.out.println("Number of times add() was used for whole program: " + Bag.addCountA + "\n");
    System.out.println("Number of times remRand() was used for whole program: " + Bag.ranRemCountA + "\n");
    System.out.println("Number of times rem() was used for whole program: " + Bag.remCountA + "\n");
    System.out.println("Number of times contains() was used whole program: " + Bag.containsCountA + "\n");
    System.out.println("number of times addAll() was used whole program: " + Bag.addAllCountA + "\n");
    System.out.println("Number of times union() was used whole program: " + Bag.unionCountA + "\n");
    System.out.println("Number of times equal() was used whole program: " + Bag.equalsCountA + "\n");
    //End timer for whole program
    final long end = System.currentTimeMillis();
    //Display timer for whole program
    System.out.println("The whole program took " + ((end - start)*.001) + " seconds to run");
}
public static <E> void runBigBag(){

    //Start time for big bag operations
    final long start2 = System.currentTimeMillis();

    boolean prog = true;

    //Create file to write bag too
    File file = new File("bag.txt");
    PrintStream ps;
    try {
        ps = new PrintStream(new FileOutputStream(file));
        System.setOut(ps);
    } catch (FileNotFoundException f) {
        f.printStackTrace();
    } finally
    //irrelevant code here


    //End timer for big bag
    final long end2 = System.currentTimeMillis();

    //Display timer for big bag
    System.out.println("This part of the program took " + ((end2 - start2)*.001) + " seconds to run");

}

}

2 Answers2

2

Using setOut is changing your default output stream so all subsequent system.out calls are sent to your file writer. If you don't want that to happen don't use the default output stream for your file writing.

the quickest answer to your problem is here. How do I create a file and write to it in Java?

Community
  • 1
  • 1
Tosh
  • 81
  • 7
0

While Tosh's answer would probably work better for me, I actually found out how to do it the way I was hoping for:

PrintStream ps;
PrintStream stdout = System.out;

I created another PrintStream that uses the default system output and at the end of the method, I put in:

System.setOut(stdout);

I'm sure this probably does something in the background I don't intend for, but gives me the output I need. Thank you for your suggestion @Tosh.