如何能夠更改System.out
我用它來檢查結果。
我需要測試這個方法。當輸出結果爲PrintStream
時,最好做到這一點。
如何能夠解決這個問題?從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);
}
}
問:
- 如何重構輸出
System.out
到PrintStream
?
不起作用。程序工作無止境。 – 2013-03-06 16:26:00
@nazar_art我不明白「Program work endless」..你能解釋一下 – asifsid88 2013-03-06 17:50:07
用'System.out'運行時間12s。有了這個變種沒有完成運行。等待很長時間,超過3分鐘。改回所有工作後好。 – 2013-03-06 17:55:01