I am using Java version 1.8.0_31.
I am trying to recursively access a directory tree using the FileVisitor interface.
The program should print the name of all files in C:/books
whose file name starts with "Ver".
The directory C:/books
has two files that starts with "Ver", Version.yxy
and Version1.txt
.
I tried using file.getFileName().startsWith("Ver")
but this returns false.
Am I missing something? Here's my code:
public class FileVisitorTest {
public static void main(String[] args) {
RetriveVersionFiles vFiles = new RetriveVersionFiles();
try {
Files.walkFileTree(Paths.get("c:", "books"), vFiles);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RetriveVersionFiles extends SimpleFileVisitor<Path> {
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
System.out.println(file.getFileName().startsWith("Ver") + " "
+ file.getFileName());
if (file.getFileName().startsWith("Ver")) {
//not entering this if block
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
}
The output of the above code is:
false Version.txt
false Version1.txt