我的文件输出被复制。每个新的 println 都包含之前的所有内容。 (包含代码)

发布于 2024-12-06 07:33:32 字数 1569 浏览 0 评论 0原文

正是我正在做的事情,

    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");
        }

我得到的一些输出是我所指的,

 算法 n 次 time/n^3 次/n^2 次/(n*log(n))

   算法n次次/n^3次/n^2次/(n*log(n))
   _________ _ (毫秒) x100,000 x10,000 x10,000


   算法n次次/n^3次/n^2次/(n*log(n))
   _________ _ (毫秒) x100,000 x10,000 x10,000

   藻类。 1 256 4.335838 0.025844 0.661596 30.543418

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");
        }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不吻晚风 2024-12-13 07:33:32

使用 String.format() 而不是 fmt.format()。或者使用 out 调用 Formatter 的构造函数并删除 outputfile.println() 调用:

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();

new Formatter() > (使用 noarg 构造函数创建)使用内部 StringBuilder 并将 format() 调用的每个结果附加到它。此外,每个 format() 返回 formatter 对象本身(return this;)。然后 outputfile.println() 调用返回的 formattertoString() 方法,该方法返回内部 StringBuilder< 的全部内容/代码>。因此,任何后续的 format() 调用都会增加 StringBuilder 的大小,并且在每次 format() 调用后打印整个缓冲区。

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.

灼疼热情 2024-12-13 07:33:32

也许你让它变得比需要的更复杂。

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

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

输出

A
B

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
稀香 2024-12-13 07:33:32

当您构造新的 Formatter 时,格式化输出的目标是 StringBuilder。因此,每次调用outputfile.println(...)时,StringBuilder的当前内容都会刷新到输出流。

要解决这个问题,您需要做的就是:


Formatter fmt = new Formatter(out);

fmt.format("%12s %12s %12s %12s %12s %12s%n", "算法", "n", "时间", "时间/n^3", "时间/n^2", "时间/(n*log(n))");

请注意,您将不再需要使用 PrintStream 对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文