开发者

My file output is being duplicated. Every new println includes all the previous. (Code included)

开发者 https://www.devze.com 2023-04-08 02:47 出处:网络
Exactly what I\'m doing, FileOutputStream out; PrintStream outputfile; try{ out = new FileOutputStream(\"Project1.txt\");

Exactly what I'm doing,

    FileOutputStream out;
    PrintStream outputfile;
    try{
        out = new FileOutputStream("Project1.txt");
        outputfile = new PrintStream(out);
        int a[ ] = { 4, -3, 5, -2, -1, 2, 6, -2 };
        int maxSum;
        Formatter fmt = new Formatter();
        //Setup table column titles
        outputfile.println(fmt.format("%12s %12s %12s %12s %12s %12s%n", "Algorithm", "n", "time", "time/n^3", "time/n^2", "time/(n*log(n))"));
        outputfile.println(fmt.format("%12s %12s %12s %12s %12s %12s%n%n", "_________", "_", "(ms)", "x100,000", "x10,000", "x10,000"));
        // Algorithm 1
        for( int n = 256; n <= 2049; n *= 2 ){
            int alg = 1;
            long timing;
            maxSum = maxSubSum1( a );
            timing = getTimingInfo( n, alg );
            double time = (((double)timing)/1000000.0);
            outputfile.println(fmt.format("%12s %12d %12f %12f %12f %12f%n", "Alg. 1", n, time, (time/(((long)n)*n*n))*100000.0, (time/(n*n))*10000, (time/((double)n*Math.log((double)n)))*10000));   
        }
        }
        catch(Exception e)
        {
          System.err.println("Error in writing to file")开发者_StackOverflow社区;
        }

Some output I am getting that I am referring to,

   Algorithm            n         time     time/n^3     time/n^2   time/(n*log(n))

   Algorithm            n         time     time/n^3     time/n^2   time/(n*log(n))
   _________            _         (ms)     x100,000      x10,000   x10,000


   Algorithm            n         time     time/n^3     time/n^2   time/(n*log(n))
   _________            _         (ms)     x100,000      x10,000   x10,000

   Alg. 1              256     4.335838     0.025844     0.661596  30.543418


Use String.format() instead of fmt.format(). Or call Formatter's constructor with out and remove the outputfile.println() calls:

out = new FileOutputStream("Project1.txt");
Formatter fmt = new Formatter(out);
fmt.format("%12s %12s %12s %12s %12s %12s%n", 
    "Algorithm", "n", "time", "time/n^3", "time/n^2", "time/(n*log(n))");
fmt.format("%12s %12s %12s %12s %12s %12s%n%n", 
    "_________", "_", "(ms)", "x100,000", "x10,000", "x10,000");

...
fmt.flush();
out.close();

A new Formatter() (created with noarg constructor) use an internal StringBuilder and appends every results of format() calls to it. Furthermore, every format() returns the formatter object itself (return this;). Then outputfile.println() calls the returned formatter's toString() method which returns the whole content of the internal StringBuilder. So, any subsequent calls of format() increases the StringBuilder's size and you print the whole buffer after every format() call.


Perhaps you are making it more complicated than it needs to be.

out.printf("%s%n", "A");
out.printf("%s%n", "B");

or

out.println("A");
out.println("B");

outputs

A
B


When you construct a new Formatter, the destination of the formatted output is a StringBuilder. Therefore, every time you call outputfile.println(...), the current content of the StringBuilder is flushed to the output stream.

To fix this problem, all you need to do is this:

Formatter fmt = new Formatter(out);

fmt.format("%12s %12s %12s %12s %12s %12s%n", "Algorithm", "n", "time", "time/n^3", "time/n^2", "time/(n*log(n))");

Notice that you will not need to use a PrintStream object any more.

0

精彩评论

暂无评论...
验证码 换一张
取 消