I think the heading says it all here...
package com.codegym.task.task31.task3111;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
/*
Advanced file search
When searching a file tree using the Files.walkFileTree(Path start, FileVisitor<? super Path> visitor) method,
we use the FileVisitor object to perform the required operations on the found Path objects.
Our search class will be called SearchFileVisitor and will extend SimpleFileVisitor.
You can search using the following criteria:
- a term found in the file name (String);
- a term found in the file's contents (String);
- a maximum or minimum file size (int).
You can specify several criteria at the same time.
I wrote code in main that uses the ready-to-use SearchFileVisitor to search for files. All you have to do is implement it. Simple!
Hint 1: The get... and set... methods are getters and setters. The search class's main logic is performed in the visitFile(Path file, BasicFileAttributes attrs) method.
Hint 2: Only use classes from the java.nio package to work with files.
Requirements:
4. If a partOfContent search criterion is passed to SearchFileVisitor,
the visitFile method must add the file to foundFiles if the file's contents contain the value of partOfContent.
5. If a maxSize search criterion is passed to SearchFileVisitor,
the visitFile method must add the file to foundFiles if the file size is less than maxSize.
6. If a minSize search criterion is passed to SearchFileVisitor,
the visitFile method must add the file to foundFiles if the file size is greater than minSize.
7. The visitFile method's implementation must allow for several search criteria to be passed at the same time.
*/
public class Solution {
public static void main(String[] args) throws IOException {
//Create new instance of SearchFileVisitor().
SearchFileVisitor searchFileVisitor = new SearchFileVisitor();
//Setting search parameters here... amigo, programmer, fileSize, etc.
searchFileVisitor.setPartOfName("amigo");
searchFileVisitor.setPartOfContent("programmer");
//searchFileVisitor.setMinSize(500);
searchFileVisitor.setMaxSize(10000);
//This initiates something relative to the visitFile() in SearchFileVisitor class.
//It seems like Paths.get() simply returns a directory from a String. Similar to File.
//walkFileTree through this folder, using the parameters of searchFileVisitor. It needs to add those files
//that match those results to its foundFiles field.
Files.walkFileTree(Paths.get("C:/Users/Mike/Desktop/stuff"), searchFileVisitor);
List<Path> foundFiles = searchFileVisitor.getFoundFiles();
for (Path file : foundFiles) {
System.out.println(file);
}
}
}