Java 将输出从System.out重构为PrintStream
如何更改我用来检查结果的Java 将输出从System.out重构为PrintStream,java,refactoring,Java,Refactoring,如何更改我用来检查结果的System.out。 我需要测试一下这个方法。最好在使用PrintStream输出时执行此操作 如何解决这个问题 代码: private void scan(File file) { Scanner scanner = null; int matches = 0; try { scanner = new Scanner(file); } catch (FileNotFoundExc
System.out
。我需要测试一下这个方法。最好在使用
PrintStream
输出时执行此操作如何解决这个问题 代码:
private void scan(File file) {
Scanner scanner = null;
int matches = 0;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
while (scanner.hasNext())
if (scanner.next().equals(whatFind)) {
matches++;
}
if (matches > 0) {
String myStr = String.format(
"File: %s - and the number of matches " + "is: %d",
file.getAbsolutePath(), matches);
System.out.println(myStr);
}
}
问题:
import java.io.PrintStream;
import java.io.PrintWriter;
public class TimeChecker
{
public static void main(String[] args)
{
/**
* Normal System.out.println
*/
long start = System.currentTimeMillis();
for(int i=1; i<1000000000; i++);
long end = System.currentTimeMillis();
System.out.println((end-start));
/**
* Using PrintWriter
*
* Note: The output is displayed only when you write "out.close()"
* Till then it's in buffer. So once you write close()
* then output is printed
*/
PrintWriter out = new PrintWriter(System.out);
start = System.currentTimeMillis();
for(int i=1; i<1000000000; i++);
end = System.currentTimeMillis();
out.println((end-start));
/**
* Using PrintStream
*/
PrintStream ps = new PrintStream(System.out, true);
System.setOut(ps);
start = System.currentTimeMillis();
for(int i=1; i<1000000000; i++);
end = System.currentTimeMillis();
ps.println((end-start));
// You need to close this for PrintWriter to display result
out.close();
}
}
- 如何将输出
重构为System.out
PrintStream
PrintWriter out=新的PrintWriter(System.out)代码>
最后别忘了关上它。
out.close()代码>
注意:out println()
比System.out.println()快。
已更新
import java.io.PrintStream;
import java.io.PrintWriter;
public class TimeChecker
{
public static void main(String[] args)
{
/**
* Normal System.out.println
*/
long start = System.currentTimeMillis();
for(int i=1; i<1000000000; i++);
long end = System.currentTimeMillis();
System.out.println((end-start));
/**
* Using PrintWriter
*
* Note: The output is displayed only when you write "out.close()"
* Till then it's in buffer. So once you write close()
* then output is printed
*/
PrintWriter out = new PrintWriter(System.out);
start = System.currentTimeMillis();
for(int i=1; i<1000000000; i++);
end = System.currentTimeMillis();
out.println((end-start));
/**
* Using PrintStream
*/
PrintStream ps = new PrintStream(System.out, true);
System.setOut(ps);
start = System.currentTimeMillis();
for(int i=1; i<1000000000; i++);
end = System.currentTimeMillis();
ps.println((end-start));
// You need to close this for PrintWriter to display result
out.close();
}
}
导入java.io.PrintStream;
导入java.io.PrintWriter;
公共类计时器
{
公共静态void main(字符串[]args)
{
/**
*正常系统.out.println
*/
长启动=System.currentTimeMillis();
对于(inti=1;i请这样尝试:PrintStream匿名对象,它不保证关闭流。但是PrintWriter保证
new PrintStream(System.out).print(str);
这是我从中得到的答案。我们不需要关闭PrintStream
?最重要的是工作不正确。如果System.out
找到10个与此变体匹配的文件,只有一个+双运行时间!@nazar\u art我不理解“程序工作无止境”…你能用System.out
运行时间12s解释一下吗?这个变量没有完成运行。等待了很长时间,超过3分钟。更改后所有工作正常。为什么会发生这种情况?如何避免这种情况?我添加了一些标识行。结果是下一步:使用System.out.println:984使用PrintStream:1469使用PrintWriter:1469
。