2011-01-31 11 views
34

का उपयोग कर रहा क्या करने की जरूरत है, तो खोज एक फ़ोल्डर का कहना है कि पता लगाएं C:\exampleएक फ़ोल्डर में फ़ाइलें जावा

मैं तो अगर यह कुछ शुरू से मेल खाता है, इसलिए यदि फ़ाइलें

शुरू देखने के लिए प्रत्येक फ़ाइल के माध्यम से जाने के लिए और जांच करने की आवश्यकता
temp****.txt 
tempONE.txt 
tempTWO.txt 

तो अगर फ़ाइल अस्थायी साथ शुरू होता है और एक विस्तार .txt मैं तो एक File file = new File("C:/example/temp***.txt); में कि फ़ाइल का नाम डाल करने के लिए तो मैं फिर फ़ाइल में पढ़ सकते हैं चाहते हैं है, पाश के बगल में स्थित फ़ाइल पर ले जाने की जरूरत है यह देखने के लिए जांचें कि क्या यह उपरोक्त के रूप में मिलता है।

उत्तर

59

आप क्या चाहते हैं 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"); 
    } 
}); 
+2

बीटीडब्ल्यू जावा स्लैश ('/') को विंडोज़ के ठीक ठीक कर सकता है। तो बैकस्लैश (और इससे बचने) की कोई ज़रूरत नहीं है। –

+1

यदि आप जावा 7 के अंतर्गत हैं, तो आप java.nio.FileSystems.getDefault() का उपयोग/उपयोग करना चाहिए। GetPath (स्ट्रिंग dir1, स्ट्रिंग dir2, ...) वास्तव में निर्देशिकाओं/फ़ाइलों को वास्तव में "बहु-प्रारूप मार्ग" – beder

+0

में संयोजित करने के लिए @ बेडर यह 'java.nio.file.FileSystems' – TheRealChx101

2

java.io.File.list() और FilenameFilter पर एक नज़र डालें।

29

तुम इतनी तरह एक 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"); 
    } 
}); 
+1

+1 है जो आपके पहले 2 मिनट पहले पोस्ट किए गए लगभग समान कोड की तुलना में थोड़ा अधिक आकर्षक चर नामों के लिए है। –

+1

@ नल सेट, वह जवाब लिखा गया था जब मैं अपना लेखन कर रहा था, इसलिए ऐसा नहीं है कि मैंने जस्टिन को लिखा और चर नाम दिया ... :) हालांकि, वोट के लिए धन्यवाद। –

+0

'+ 1' ठीक उसी तरह सोचने के लिए मैंने किया। – jjnguy

3

पर विचार करें अपाचे कॉमन्स आईओ, यह एक वर्ग FileUtils नामक एक listFiles विधि है कि आपके मामले में बहुत उपयोगी हो सकता है है है।

1

Appache कॉमन्स आईओ विभिन्न

FilenameUtils.wildcardMatch

अपाचे देखें जावाडोक here। यह फ़ाइल नाम के साथ वाइल्डकार्ड से मेल खाता है। तो आप अपनी तुलना के लिए इस विधि का उपयोग कर सकते हैं।

+0

है इसलिए संक्षेप में उत्तर कम गुणवत्ता के रूप में फ़्लैग किए जाने की संभावना है। – joaquin

+0

मेरी प्रतिष्ठा मुझे टिप्पणी जोड़ने की अनुमति नहीं देती है। एकमात्र तरीका मैं योगदान दे सकता हूं एक और जवाब जोड़ना है। मैंने अधिक जानकारी जोड़ने के लिए अपनी मूल पोस्ट संपादित की। – eugene

2

आपकी दी गई निर्देशिका से जेसन फ़ाइलों को सूचीबद्ध करने के लिए।

import java.io.File; 
    import java.io.FilenameFilter; 

    public class ListOutFilesInDir { 
     public static void main(String[] args) throws Exception { 

      File[] fileList = getFileList("directory path"); 

      for(File file : fileList) { 
       System.out.println(file.getName()); 
      } 
     } 

     private static File[] getFileList(String dirPath) { 
      File dir = new File(dirPath); 

      File[] fileList = dir.listFiles(new FilenameFilter() { 
       public boolean accept(File dir, String name) { 
        return name.endsWith(".json"); 
       } 
      }); 
      return fileList; 
     } 
    } 
1

रूप @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(); 
} 
5

मैं जानता हूँ कि उलटे पांव लौटने से, यह कोई पुराना सवाल है। लेकिन सिर्फ पूर्णता के लिए, लैम्ब्डा संस्करण।

File dir = new File(directory); 
File[] files = dir.listFiles((dir1, name) -> name.startsWith("temp") && name.endsWith(".txt")); 
संबंधित मुद्दे