रूप @Clarke कहा, तुम java.io.FilenameFilter
उपयोग कर सकते हैं विशिष्ट स्थिति से फ़ाइल फिल्टर करने के लिए।
एक पूरक के रूप में, मैं यह दिखाना चाहता हूं कि वर्तमान निर्देशिका और इसकी उपनिर्देशिका में फ़ाइल को खोजने के लिए java.io.FilenameFilter
का उपयोग कैसे करें।
सामान्य तरीकों को प्राप्त करने के लिए TargetFiles और 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);
}
}
}
मैं कैसे पुनरावर्ती, 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);
}
चौड़ाई पहले खोज:
/**
* 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();
}
गहराई पहले खोज:
/** * खोज जहाँ तक प्रत्येक के साथ संभव के रूप में शाखा बीएफ अयस्क */ निजी शून्य 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();
}
बीटीडब्ल्यू जावा स्लैश ('/') को विंडोज़ के ठीक ठीक कर सकता है। तो बैकस्लैश (और इससे बचने) की कोई ज़रूरत नहीं है। –
यदि आप जावा 7 के अंतर्गत हैं, तो आप java.nio.FileSystems.getDefault() का उपयोग/उपयोग करना चाहिए। GetPath (स्ट्रिंग dir1, स्ट्रिंग dir2, ...) वास्तव में निर्देशिकाओं/फ़ाइलों को वास्तव में "बहु-प्रारूप मार्ग" – beder
में संयोजित करने के लिए @ बेडर यह 'java.nio.file.FileSystems' – TheRealChx101