How to get a files with a certain extension from a folder?

Here is an example to expalin how to get a files with a certain extension from a folder.

  • First example:
package com.nourit.javaIO;

import java.io.File;

public class FindFilesWithCertainExtension {

  public static void main(String[] args) {
    File folder = new File("D:/tmp");
    String ext = ".tmp";
    File fList[] = folder.listFiles();
    // Searchs .tmp
    for (int i = 0; i < fList.length; i++) {
        String temp = fList[i].getPath();
        if (temp.endsWith(ext)) {
            // and printed
            System.out.println("file : " + temp + " is here in : "+ folder.getPath());
        }
    }
  }
}
  • Second Example:
package com.nourit.javaIO;

import java.io.File;

public class FindFilesWithCertainExtension2 {

  public static void main(String[] args) {
    File folder = new File("D:/tmp");
    String ext = ".tmp";
    // Searchs .tmp
    for (File f : folder.listFiles()) {
      if (f.getName().endsWith(ext)) {
         // and printed
            System.out.println("file : " + f.getPath() + " is here in : "+ folder.getPath());
      }
    }
  }
}

Output:

file : D:\tmp\temp-nourit-file-name7880135018657413763.tmp is here in : D:\tmp

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*