Java Reference
In-Depth Information
* @throws FileNotFoundException
*/
public
public
void
throws
FileNotFoundException
{
iif
(!
file
.
exists
() || !
file
.
canRead
()) {
System
.
err
.
println
(
"ERROR: can't read file "
+
file
.
getAbsolutePath
());
return
void
process
(
File file
)
throws
return
;
}
iif
(
file
.
isFile
()) {
process
(
new
new
FileReader
(
file
)),
file
.
getAbsolutePath
());
return
new
BufferedReader
(
new
return
;
}
iif
(
file
.
isDirectory
()) {
iif
(!
recursive
) {
System
.
err
.
println
(
"ERROR: -r not specified but directory given "
+
file
.
getAbsolutePath
());
return
return
;
}
for
for
(
File nf
:
file
.
listFiles
()) {
process
(
nf
);
// "Recursion, n.: See Recursion."
}
return
return
;
}
System
.
err
.
println
(
"WEIRDNESS: neither file nor directory: "
+
file
.
getAbsolutePath
());
}
/** Do the work of scanning one file
* @param ifile Reader Reader object already open
* @param fileName String Name of the input file
*/
public
public
void
void
process
(
Reader ifile
,
String fileName
) {
String inputLine
;
int
int
matches
=
0
;
try
try
(
BufferedReader reader
=
new
new
BufferedReader
(
ifile
)) {
while
while
((
inputLine
=
reader
.
readLine
()) !=
null
null
) {
matcher
.
reset
(
inputLine
);
iif
(
matcher
.
find
()) {
iif
(
listOnly
) {
// -l, print filename on first match, and we're done
System
.
out
.
println
(
fileName
);