Home > Java > javaTutorial > body text

How to search a directory with a specific file extension in Java?

WBOY
Release: 2023-08-31 08:13:11
forward
1404 people have browsed it

How to search a directory with a specific file extension in Java?

The following example prints the files in a directory based on the extension -

Example

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) throws IOException {
      Stream<Path> path = Files.walk(Paths.get("D:\ExampleDirectory"));
      System.out.println("List of PDF files:");
      path = path.filter(var -> var.toString().endsWith(".pdf"));
      path.forEach(System.out::println);      
      path = Files.walk(Paths.get("D:\ExampleDirectory"));
      System.out.println("List of JPG files:");
      path = path.filter(var -> var.toString().endsWith(".jpg"));
      path.forEach(System.out::println);    
      path = Files.walk(Paths.get("D:\ExampleDirectory"));
      System.out.println("List of text files:");
      path = path.filter(var -> var.toString().endsWith(".txt"));
      path.forEach(System.out::println);      
      path = Files.walk(Paths.get("D:\ExampleDirectory"));
      System.out.println("List of word files:");
      path = path.filter(var -> var.toString().endsWith(".docx"));
      path.forEach(System.out::println);      
    }
}
Copy after login

Output

List of PDF files:
D:\ExampleDirectory\demo1.pdf
D:\ExampleDirectory\demo2.pdf
List of JPG files:
D:\ExampleDirectory\sample_jpeg1.jpg
D:\ExampleDirectory\sample_jpeg2.jpg
List of text files:
D:\ExampleDirectory\sample1.txt
D:\ExampleDirectory\sample2.txt
D:\ExampleDirectory\sample3.txt
List of word files:
D:\ExampleDirectory\test1.docx
D:\ExampleDirectory\test2.docx
Copy after login

The following example prints the files based on the extension Print the names of PDF files in a directory -

Example

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
public class MyExample{
   public static void main(String args[]) throws IOException {
    //Creating a File object for directory
    File directoryPath = new File("D:\ExampleDirectory");
    //Creating filter for jpg files
    FilenameFilter jpgFilefilter = new FilenameFilter(){
         public boolean accept(File dir, String name) {
            String lowercaseName = name.toLowerCase();
            if (lowercaseName.endsWith(".pdf")) {
               return true;
            } else {
               return false;
            }
         }
      };        
      String imageFilesList[] = directoryPath.list(jpgFilefilter);
      System.out.println("List of the jpeg files in the specified directory:");  
      for(String fileName : imageFilesList) {
         System.out.println(fileName);
      }  
   }
}
Copy after login

Output

List of the jpeg files in the specified directory:
demo1.pdf
demo2.pdf
Copy after login

The above is the detailed content of How to search a directory with a specific file extension in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!