Sii*_*Sii 47 java directory search for-loop file
如果搜索文件夹说我需要做什么 C:\example
然后,我需要浏览每个文件并检查它是否与几个起始字符匹配,以便文件启动
temp****.txt
tempONE.txt
tempTWO.txt
Run Code Online (Sandbox Code Playgroud)
所以如果文件以temp开头并且有一个扩展名.txt我想把那个文件名放到一个File file = new File("C:/example/temp***.txt);所以我可以在文件中读取,然后循环需要移动到下一个文件以检查它是否满足如上.
jjn*_*guy 70
你想要的是什么File.listFiles(FileNameFilter filter).
这将为您提供所需目录中与某个筛选器匹配的文件列表.
代码看起来类似于:
// your directory
File f = new File("C:\\example");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("temp") && name.endsWith("txt");
}
});
Run Code Online (Sandbox Code Playgroud)
Mia*_*rke 32
您可以使用FilenameFilter,如下所示:
File dir = new File(directory);
File[] matches = dir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.startsWith("temp") && name.endsWith(".txt");
}
});
Run Code Online (Sandbox Code Playgroud)
pan*_*pan 17
我知道,这是一个老问题.但仅仅为了完整性,lambda版本.
File dir = new File(directory);
File[] files = dir.listFiles((dir1, name) -> name.startsWith("temp") && name.endsWith(".txt"));
Run Code Online (Sandbox Code Playgroud)
正如@Clarke 所说,您可以使用java.io.FilenameFilter按特定条件过滤文件。
作为补充,我想展示如何使用java.io.FilenameFilter搜索当前目录及其子目录中的文件。
常用方法 getTargetFiles 和 printFiles 用于搜索文件并打印它们。
public class SearchFiles {
//It's used in dfs
private Map<String, Boolean> map = new HashMap<String, Boolean>();
private File root;
public SearchFiles(File root){
this.root = root;
}
/**
* List eligible files on current path
* @param directory
* The directory to be searched
* @return
* Eligible files
*/
private String[] getTargetFiles(File directory){
if(directory == null){
return null;
}
String[] files = directory.list(new FilenameFilter(){
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.startsWith("Temp") && name.endsWith(".txt");
}
});
return files;
}
/**
* Print all eligible files
*/
private void printFiles(String[] targets){
for(String target: targets){
System.out.println(target);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将演示如何使用recursive、bfs和dfs来完成工作。
递归:
/**
* How many files in the parent directory and its subdirectory <br>
* depends on how many files in each subdirectory and their subdirectory
*/
private void recursive(File path){
printFiles(getTargetFiles(path));
for(File file: path.listFiles()){
if(file.isDirectory()){
recursive(file);
}
}
if(path.isDirectory()){
printFiles(getTargetFiles(path));
}
}
public static void main(String args[]){
SearchFiles searcher = new SearchFiles(new File("C:\\example"));
searcher.recursive(searcher.root);
}
Run Code Online (Sandbox Code Playgroud)
广度优先搜索:
/**
* Search the node's neighbors firstly before moving to the next level neighbors
*/
private void bfs(){
if(root == null){
return;
}
Queue<File> queue = new LinkedList<File>();
queue.add(root);
while(!queue.isEmpty()){
File node = queue.remove();
printFiles(getTargetFiles(node));
File[] childs = node.listFiles(new FileFilter(){
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
if(pathname.isDirectory())
return true;
return false;
}
});
if(childs != null){
for(File child: childs){
queue.add(child);
}
}
}
}
public static void main(String args[]){
SearchFiles searcher = new SearchFiles(new File("C:\\example"));
searcher.bfs();
}
Run Code Online (Sandbox Code Playgroud)
深度优先搜索:
/**
* Search as far as possible along each branch before backtracking
*/
private void dfs(){
if(root == null){
return;
}
Stack<File> stack = new Stack<File>();
stack.push(root);
map.put(root.getAbsolutePath(), true);
while(!stack.isEmpty()){
File node = stack.peek();
File child = getUnvisitedChild(node);
if(child != null){
stack.push(child);
printFiles(getTargetFiles(child));
map.put(child.getAbsolutePath(), true);
}else{
stack.pop();
}
}
}
/**
* Get unvisited node of the node
*
*/
private File getUnvisitedChild(File node){
File[] childs = node.listFiles(new FileFilter(){
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
if(pathname.isDirectory())
return true;
return false;
}
});
if(childs == null){
return null;
}
for(File child: childs){
if(map.containsKey(child.getAbsolutePath()) == false){
map.put(child.getAbsolutePath(), false);
}
if(map.get(child.getAbsolutePath()) == false){
return child;
}
}
return null;
}
public static void main(String args[]){
SearchFiles searcher = new SearchFiles(new File("C:\\example"));
searcher.dfs();
}
Run Code Online (Sandbox Code Playgroud)