Search.java
package search;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
public class Search {
public static void main(String[] args) {
String path = "C:\\Users\\";
String word = "\\pool\\plugins\\";
if (validate(path, word)) {
//Recursive search when input value is OK
search(path, word);
}
System.out.println("end");
}
/**
*Search the directory by string.
* <p>
*While outputting the absolute path of the file and directory with the name including the search string
*Search recursively.
*
* @param dir Search target directory
* @param word Search string
* @throws IOException When something doesn't open
*/
private static void search(String dir, String word) {
//Get the list in the directory
File[] directory = new File(dir).listFiles();
//If the object does not exist,End processing
if (directory == null) {
return;
}
//Outputs absolute path for those containing search words
Arrays.stream(directory).map(File::getName).filter(file -> isMatch(file, word))
.forEach(file -> System.out.println(dir + "\\" + file));
//Recursive search of directories
Arrays.stream(directory).filter(File::isDirectory).map(File::getPath)
.forEach(nextPath -> search(nextPath, word));
}
/**
*Word search.
* <p>
*Separate the characters to be searched with a space,OR search.
* <p>
*If the file name contains one of the words{@code ture}
*
* @param file filename
* @param word word to search
* @If you include return{@code true}
*/
private static boolean isMatch(String file, String word) {
List<String> stock = new LinkedList<>();
//Separated by whitespace
for (String e : word.split(" ")) {
// \\Separated by
for (String e2 : e.split("\\\\")) {
stock.add(e2);
}
}
//True if it contains a string
return stock.stream().map(String::trim).anyMatch(spell -> file.contains(spell));
}
/**
*Check for null blanks.
* <ul>
*Check contents
* <li>The argument is{@code null}For an array of{@code false}
* <li>If the following is satisfied for all the elements of the argument{@code true}
* <ul>
* <li>{@code null}Not
* <li>{@code String}Not empty string when converted
* <li>{@code String}Not a whitespace character when converted
* </ul>
*If even one gets caught{@code false}
* </ul>
*
* @param obj {@code null}Object array that allows
* @return check availability
*/
private static boolean validate(Object...obj) {
return obj == null ? false
: Arrays.stream(obj)
.filter(Objects::nonNull)
.map(Object::toString)
.filter(nonEmpty)
.filter(nonBlank)
//True if all is OK
.count() == obj.length ? true : false;
}
/**True if not empty*/
private static final Predicate<String> nonEmpty = str -> !str.equals("");
/**True if only blank*/
private static final Predicate<String> nonBlank = v -> v != null && !v.equals("")
&& Arrays.stream(v.split("")).anyMatch(x -> !x.equals(" ") && !x.equals(" "));
}
It's cheap that the output is a console
OR search for multiple words separated by spaces
Well, the search process is also a little appropriate, so please don't dig into the ʻisMatchmethod. .. I could have just used
String.contains`, but I wonder if it is necessary to devise various things such as web systems.
The validate
part is also adapted to this process, so it may not be said to be versatile.
So the main subject
search.java
/**
*Search the directory by string.
* <p>
*While outputting the absolute path of the file and directory with the name including the search string
*Search recursively.
*
* @param dir Search target directory
* @param word Search string
*/
private static void search(String dir, String word) {
//Get the list in the directory
File[] directory = new File(dir).listFiles();
//If the object does not exist,End processing
if (directory == null) {
return;
}
//Outputs absolute path for those containing search words
Arrays.stream(directory).map(File::getName).filter(file -> isMatch(file, word))
.forEach(file -> System.out.println(dir + "\\" + file));
//Recursive search of directories
Arrays.stream(directory).filter(File::isDirectory).map(File::getPath)
.forEach(nextPath -> search(nextPath, word));
}
This is interesting for recursive search
Find the one in the directory from the list Get the path Do the same with it as an argument
If there is no return statement in the middle, it will be an infinite loop. Is it a while statement that does not use while?
Calling yourself is really interesting!
Recommended Posts