我的文件输出被复制。每个新的 println 都包含之前的所有内容。 (包含代码)
正是我正在做的事情,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
String.format()
而不是fmt.format()
。或者使用out
调用Formatter
的构造函数并删除outputfile.println()
调用:new Formatter()
> (使用 noarg 构造函数创建)使用内部StringBuilder
并将format()
调用的每个结果附加到它。此外,每个format()
返回formatter
对象本身(return this;
)。然后outputfile.println()
调用返回的formatter
的toString()
方法,该方法返回内部StringBuilder< 的全部内容/代码>。因此,任何后续的
format()
调用都会增加StringBuilder
的大小,并且在每次format()
调用后打印整个缓冲区。Use
String.format()
instead offmt.format()
. Or callFormatter
's constructor without
and remove theoutputfile.println()
calls:A
new Formatter()
(created with noarg constructor) use an internalStringBuilder
and appends every results offormat()
calls to it. Furthermore, everyformat()
returns theformatter
object itself (return this;
). Thenoutputfile.println()
calls the returnedformatter
'stoString()
method which returns the whole content of the internalStringBuilder
. So, any subsequent calls offormat()
increases theStringBuilder
's size and you print the whole buffer after everyformat()
call.也许你让它变得比需要的更复杂。
或
输出
Perhaps you are making it more complicated than it needs to be.
or
outputs
当您构造新的
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 aStringBuilder
. Therefore, every time you calloutputfile.println(...)
, the current content of theStringBuilder
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.