3

I am writing a method to get specific file type such as pdf or txt from folders and subfolders but I am lacking to solve this problem. here is my code

  // .............list file
    File directory = new File(directoryName);

    // get all the files from a directory
    File[] fList = directory.listFiles();

    for (File file : fList) {
        if (file.isFile()) {
            System.out.println(file.getAbsolutePath());
        } else if (file.isDirectory()) {
            listf(file.getAbsolutePath());
        }
    }

My current method list all files but I need specific files

user2614607
  • 113
  • 2
  • 6
  • 17
  • 1
    Google for FileFilter or see http://stackoverflow.com/questions/5603966/how-to-make-filefilter-in-java – vickirk Dec 05 '13 at 13:47

6 Answers6

10

For a filtered list without needing recursion through sub directories you can just do:

directory.listFiles(new FilenameFilter() {
    boolean accept(File dir, String name) {
        return name.endsWith(".pdf");
    }});

For efficiency you could create the FilenameFilter ahead of time rather than for each call.

In this case because you want to scan sub folders too there is no point filtering the files as you still need to check for sub folders. In fact you were very nearly there:

File directory = new File(directoryName);

// get all the files from a directory
File[] fList = directory.listFiles();

for (File file : fList) {
    if (file.isFile()) {
       if (file.getName().endsWith(".pdf")) {
           System.out.println(file.getAbsolutePath());
       }
    } else if (file.isDirectory()) {
        listf(file.getAbsolutePath());
    }
}
Tim B
  • 38,707
  • 15
  • 73
  • 123
  • i tried your logic but it works only with root directory not with sub folders ... – user2614607 Dec 05 '13 at 13:56
  • Ahh, sorry. I missed the requirement for recursion. Yes you are right, this will not list sub folders (unless they happen to end with .pdf). In this case since you need to scan all the files anyway going with the other suggestions of just checking the filename is probably your simplest suggestion. - I'll edit my answer. – Tim B Dec 05 '13 at 14:22
  • What is "listf(file.getAbsolutePath())"? I cannot seem to resolve listf – Lismore Jan 17 '17 at 14:06
  • 1
    @Lismore create a method called listf - put this as the contents of the method. – Tim B Jan 17 '17 at 15:03
6
if(file.getName().endsWith(".pdf")) {
    //it is a .pdf file!
}

/***/

Community
  • 1
  • 1
Memran
  • 415
  • 4
  • 12
2

Try using the FilenameFilter interface in you function http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html

http://www.mkyong.com/java/how-to-find-files-with-certain-extension-only/ - for a code that has extention filter

asafm
  • 861
  • 6
  • 15
1

Use File.listFiles(FileFilter).

Example:

File[] fList = directory.listFiles(new FileFilter() {
    @Override
    public boolean accept(File file) {
        return file.getName().endSwith(".pdf");
    }
});
Seelenvirtuose
  • 19,157
  • 6
  • 34
  • 57
0

You can use apache fileUtils class

String[] exte= {"xml","properties"};
Collection<File> files = FileUtils.listFiles(new File("d:\\workspace"), exte, true);

for(File file: files){
     System.out.println(file.getAbsolutePath());
}
Karthik Prasad
  • 8,604
  • 7
  • 53
  • 93
0

My advice is to use FileUtils or NIO.2.
NIO.2 allows Stream with Depth-First search, for example you can print all files with a specified extension in one line of code:

Path path = Path.get("/folder");
try{
    Files.walk(path).filter(n -> n.toString().endsWith(".extension")).forEach(System.out::println)
}catch(IOException e){
    //Manage exception
}
luca
  • 2,878
  • 7
  • 54
  • 120