Refactor FileSearchExample
This commit is contained in:
parent
0e3b7fe17c
commit
211d769942
|
@ -1,23 +1,19 @@
|
||||||
package com.baeldung.java.nio2.visitor;
|
package com.baeldung.java.nio2.visitor;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.*;
|
||||||
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
|
|
||||||
import static java.nio.file.FileVisitResult.CONTINUE;
|
import static java.nio.file.FileVisitResult.CONTINUE;
|
||||||
import static java.nio.file.FileVisitResult.TERMINATE;
|
import static java.nio.file.FileVisitResult.TERMINATE;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.FileVisitResult;
|
|
||||||
import java.nio.file.FileVisitor;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
|
||||||
|
|
||||||
public class FileSearchExample implements FileVisitor<Path> {
|
public class FileSearchExample implements FileVisitor<Path> {
|
||||||
private static String FILE_NAME;
|
private final String fileName;
|
||||||
private static Path START_DIR;
|
private final Path startDir;
|
||||||
|
|
||||||
public FileSearchExample(String fileName, Path startingDir) {
|
public FileSearchExample(String fileName, Path startingDir) {
|
||||||
FILE_NAME = fileName;
|
this.fileName = fileName;
|
||||||
START_DIR = startingDir;
|
startDir = startingDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -28,7 +24,7 @@ public class FileSearchExample implements FileVisitor<Path> {
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
String fileName = file.getFileName().toString();
|
String fileName = file.getFileName().toString();
|
||||||
if (FILE_NAME.equals(fileName)) {
|
if (this.fileName.equals(fileName)) {
|
||||||
System.out.println("File found: " + file.toString());
|
System.out.println("File found: " + file.toString());
|
||||||
return TERMINATE;
|
return TERMINATE;
|
||||||
}
|
}
|
||||||
|
@ -43,9 +39,9 @@ public class FileSearchExample implements FileVisitor<Path> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||||
boolean finishedSearch = Files.isSameFile(dir, START_DIR);
|
boolean finishedSearch = Files.isSameFile(dir, startDir);
|
||||||
if (finishedSearch) {
|
if (finishedSearch) {
|
||||||
System.out.println("File:" + FILE_NAME + " not found");
|
System.out.println("File:" + fileName + " not found");
|
||||||
return TERMINATE;
|
return TERMINATE;
|
||||||
}
|
}
|
||||||
return CONTINUE;
|
return CONTINUE;
|
||||||
|
|
Loading…
Reference in New Issue