beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Find files with given extension

By Chaitanya Singh | Filed Under: Java I/O

In this tutorial, we will see how to find all the files with certain extensions in the specified directory.

Program: Searching all files with “.png” extension

In this program, we are searching all “.png” files in the “Documents” directory(folder). I have placed three files Image1.png, Image2.png, Image3.png in the Documents directory.

Similarly, we can search files with the other extensions like “.jpeg”,”jpg”,”.xml”,”.txt” etc.

package com.beginnersbook;
import java.io.*;

public class FindFilesWithThisExtn {
	//specify the path (folder) where you want to search files
	private static final String fileLocation = "/Users/chaitanyasingh/Documents";
	
	//extension you want to search for e.g. .png, .jpeg, .xml etc
	private static final String searchThisExtn = ".png";

	public static void main(String args[]) {
		FindFilesWithThisExtn obj = new FindFilesWithThisExtn();
		obj.listFiles(fileLocation, searchThisExtn);
	}

	public void listFiles(String loc, String extn) {

		SearchFiles files = new SearchFiles(extn);

		File folder = new File(loc);

		if(folder.isDirectory()==false){
			System.out.println("Folder does not exists: " + fileLocation);
			return;
		}

		String[] list = folder.list(files);

		if (list.length == 0) {
			System.out.println("There are no files with " + extn + " Extension");
			return;
		}

		for (String file : list) {
			String temp = new StringBuffer(fileLocation).append(File.separator)
					.append(file).toString();
			System.out.println("file : " + temp);
		}
	}

	public class SearchFiles implements FilenameFilter {

		private String ext;

		public SearchFiles(String ext) {
			this.ext = ext;
		}
		
		@Override
		public boolean accept(File loc, String name) {
			if(name.lastIndexOf('.')>0)
            {
               // get last index for '.' 
               int lastIndex = name.lastIndexOf('.');
               
               // get extension
               String str = name.substring(lastIndex);
               
               // matching extension 
               if(str.equalsIgnoreCase(ext))
               {
                  return true;
               }
            }
            return false;
			
		}
	}
}

Output:

file : /Users/chaitanyasingh/Documents/Image1.png
file : /Users/chaitanyasingh/Documents/Image2.png
file : /Users/chaitanyasingh/Documents/Image3.png

Enjoyed this post? Try these related posts

  1. How to read file in Java – BufferedInputStream
  2. How to check if a File is hidden in Java
  3. How to read file in Java using BufferedReader
  4. Append to a file in java using BufferedWriter, PrintWriter, FileWriter
  5. How to Copy a File to another File in Java
  6. How to Compress a File in GZIP Format

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap