How able to change System.out
which I use to check the result.
I need test this method. Better do this when output will be with PrintStream
.
How able to solve this?
Code:
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);
}
}
Question:
- How to refactor output
System.out
toPrintStream
?