How to Find Files with Given Extension in Java


In this example we will show the method to find files with certain extension in Java.

Source Code

package com.beginner.examples;

import java.io.*;

public class FileOperator10Example { 
	public static void main(String[] args )
    {	
		File path = new File("example");
		File file = null;
		File[] filelist = path.listFiles();
		for (int i = 0; i < filelist.length; i++) 
		{
		    file = filelist[i];
		    if (file.getName().endsWith(".txt"))
			{
			    System.out.println(file + " is found.");
			}
		}
    }
}

Output:

exampletest.txt is found.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments