Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another alternative is to using the FileVisitor interface introduced in JDK7 to perform the search. The link at <a href="http://docs.oracle.com/javase/tutorial/essential/io/walk.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/essential/io/walk.html</a> provides details on how to use the FileVisitor interface.</p> <p>The following code block is a re implementation of the search that should be able to list files at a windows drive level in addition to normal directories. Note that the implementation uses Files.walkTree method that is provided as part of the NIO 2 File IO Operations.</p> <pre><code>public class FileSearch { static String fd; static boolean flg = true; static class FileSearchVisitor extends SimpleFileVisitor&lt;Path&gt; { private final Path pathToSearch; boolean found; FileSearchVisitor(Path pathToSearch) { found = false; this.pathToSearch = pathToSearch; } public boolean isFound() { return found; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { super.preVisitDirectory(dir, attrs); if (pathToSearch.getFileName().equals(dir.getFileName())) { System.out.println("Found " + pathToSearch); found = true; } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { super.visitFile(file, attrs); if (pathToSearch.getFileName().equals(file.getFileName())) { System.out.println("Found " + pathToSearch); found = true; } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) { System.err.println("Visit failed for file at path : " + file); exc.printStackTrace(); return FileVisitResult.CONTINUE; } } public static void main(String arr[]) throws Exception { fd = arr[0]; String path = arr[1]; final Path searchFile = Paths.get(fd); Path filePath = Paths.get(path); FileSearchVisitor visitor = new FileSearchVisitor(searchFile); Files.walkFileTree(filePath, visitor); if (!visitor.isFound()) { System.out.print("File not found."); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload